5
5
using Microsoft . Extensions . Configuration ;
6
6
using Microsoft . Extensions . Hosting ;
7
7
using Microsoft . IdentityModel . Tokens ;
8
+ using Microsoft . OpenApi . Models ;
9
+ using Swashbuckle . AspNetCore . SwaggerGen ;
8
10
using System . IdentityModel . Tokens . Jwt ;
9
11
10
12
namespace BotSharp . OpenAPI ;
@@ -18,8 +20,8 @@ public static class BotSharpOpenApiExtensions
18
20
/// <param name="services"></param>
19
21
/// <param name="config"></param>
20
22
/// <returns></returns>
21
- public static IServiceCollection AddBotSharpOpenAPI ( this IServiceCollection services ,
22
- IConfiguration config ,
23
+ public static IServiceCollection AddBotSharpOpenAPI ( this IServiceCollection services ,
24
+ IConfiguration config ,
23
25
string [ ] origins ,
24
26
IHostEnvironment env ,
25
27
bool enableValidation )
@@ -62,7 +64,31 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
62
64
63
65
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
64
66
services . AddEndpointsApiExplorer ( ) ;
65
- services . AddSwaggerGen ( ) ;
67
+ services . AddSwaggerGen (
68
+ c =>
69
+ {
70
+ c . AddSecurityDefinition ( "Bearer" , new OpenApiSecurityScheme
71
+ {
72
+ In = ParameterLocation . Header ,
73
+ Description = "Please insert JWT with Bearer into field" ,
74
+ Name = "Authorization" ,
75
+ Type = SecuritySchemeType . ApiKey
76
+ } ) ;
77
+ c . AddSecurityRequirement ( new OpenApiSecurityRequirement {
78
+ {
79
+ new OpenApiSecurityScheme
80
+ {
81
+ Reference = new OpenApiReference
82
+ {
83
+ Type = ReferenceType . SecurityScheme ,
84
+ Id = "Bearer"
85
+ }
86
+ } ,
87
+ Array . Empty < string > ( )
88
+ }
89
+ } ) ;
90
+ }
91
+ ) ;
66
92
67
93
services . AddHttpContextAccessor ( ) ;
68
94
@@ -94,6 +120,7 @@ public static IApplicationBuilder UseBotSharpOpenAPI(this IApplicationBuilder ap
94
120
app . UseCors ( policy ) ;
95
121
96
122
app . UseSwagger ( ) ;
123
+
97
124
if ( env . IsDevelopment ( ) )
98
125
{
99
126
app . UseSwaggerUI ( ) ;
@@ -103,7 +130,7 @@ public static IApplicationBuilder UseBotSharpOpenAPI(this IApplicationBuilder ap
103
130
app . UseAuthentication ( ) ;
104
131
105
132
app . UseRouting ( ) ;
106
-
133
+
107
134
app . UseAuthorization ( ) ;
108
135
109
136
app . UseEndpoints (
@@ -150,3 +177,4 @@ public static IApplicationBuilder UseBotSharpUI(this IApplicationBuilder app, bo
150
177
return app ;
151
178
}
152
179
}
180
+
0 commit comments