-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwaggerServiceExtensions.cs
More file actions
74 lines (61 loc) · 2.56 KB
/
SwaggerServiceExtensions.cs
File metadata and controls
74 lines (61 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Microsoft.AspNetCore.Builder;
using Microsoft.OpenApi.Models;
namespace Microsoft.Extensions.DependencyInjection;
public static class SwaggerServiceExtensions
{
private static string _applicationName = string.Empty;
private static string _applicationVersion = string.Empty;
public static IServiceCollection AddCoreSwaggerWithJWT(this IServiceCollection services, string applicationName = "API", string applicationVersion = "v.1.0.0")
{
_applicationName = applicationName;
_applicationVersion = applicationVersion;
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.OrderActionsBy(x => x.RelativePath);
c.SwaggerDoc("v1", new OpenApiInfo { Title = applicationName, Version = applicationVersion });
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
In = ParameterLocation.Header,
Description = @"JWT Authorization header using the Bearer scheme.
Enter 'Bearer' [space] and then your token in the text input below.
nExample: 'Bearer 12345abcdef'",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
var securityKeyScheme = new OpenApiSecurityScheme()
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header
};
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
securityKeyScheme, new List<string>()
}
});
});
//services.AddSwaggerGenNewtonsoftSupport();
return services;
}
public static IApplicationBuilder UseCoreSwagger(this IApplicationBuilder app, string routePrefix = "doc")
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.DisplayRequestDuration();
c.SwaggerEndpoint("/swagger/v1/swagger.json", _applicationName + ", Version " + _applicationVersion);
c.RoutePrefix = routePrefix;
});
return app;
}
}