2
2
using Microsoft . AspNetCore . Authentication . JwtBearer ;
3
3
using Microsoft . AspNetCore . Builder ;
4
4
using Microsoft . AspNetCore . Identity ;
5
+ using Microsoft . AspNetCore . Mvc ;
6
+ using Microsoft . AspNetCore . Mvc . ApiExplorer ;
7
+ using Microsoft . AspNetCore . Mvc . Versioning ;
5
8
using Microsoft . EntityFrameworkCore ;
6
9
using Microsoft . Extensions . Configuration ;
7
10
using Microsoft . Extensions . DependencyInjection ;
13
16
using Supermarket . Persistent . Context ;
14
17
using Supermarket . Persistent . Contracts ;
15
18
using Supermarket . Persistent . Repositories ;
19
+ using Supermarket . Swagger ;
16
20
using Swashbuckle . AspNetCore . Swagger ;
17
21
using System ;
22
+ using System . Collections . Generic ;
23
+ using System . Linq ;
18
24
using System . Text ;
19
25
20
26
namespace Supermarket . Extensions
@@ -66,28 +72,34 @@ public static void ConfigureAutoMapper(this IServiceCollection services)
66
72
services . AddAutoMapper ( ) ;
67
73
}
68
74
69
- public static void ConfigureSwagger ( this IServiceCollection services )
75
+ public static void ConfigureSwagger ( this IServiceCollection services , Type type )
70
76
{
71
- services . AddSwaggerGen ( c =>
77
+ services . AddSwaggerGen ( options =>
72
78
{
73
- c . SwaggerDoc ( "v1" , new Info
79
+ // Resolve the temprary IApiVersionDescriptionProvider service
80
+ var provider = services . BuildServiceProvider ( ) . GetRequiredService < IApiVersionDescriptionProvider > ( ) ;
81
+
82
+ // Add a swagger document for each discovered API version
83
+ foreach ( var description in provider . ApiVersionDescriptions )
84
+ {
85
+ options . SwaggerDoc ( description . GroupName , SwaggerInfo . CreateInfoForApiVersion ( description , type ) ) ;
86
+ }
87
+
88
+ // Define the BearerAuth scheme that's in use
89
+ options . AddSecurityDefinition ( "Bearer" , new ApiKeyScheme ( )
90
+ {
91
+ Description = "JWT Authorization header using the Bearer scheme. Example: \" Authorization: Bearer {token}\" " ,
92
+ Name = "Authorization" ,
93
+ In = "header" ,
94
+ Type = "apiKey"
95
+ } ) ;
96
+ options . AddSecurityRequirement ( new Dictionary < string , IEnumerable < string > >
74
97
{
75
- Title = "Supermarket API" ,
76
- Version = "v1" ,
77
- Description = "A simple example ASP.NET Core Web API" ,
78
- TermsOfService = "None" ,
79
- Contact = new Contact
80
- {
81
- Name = "Mathavan N" ,
82
- Email = "mathavan@gmail.com" ,
83
- Url = "https://github.com/Mathavana"
84
- } ,
85
- License = new License
86
- {
87
- Name = "Use under LICX" ,
88
- Url = "https://example.com/license"
89
- }
98
+ { "Bearer" , Enumerable . Empty < string > ( ) } ,
90
99
} ) ;
100
+
101
+ // Add a custom filter for setting the default values
102
+ options . OperationFilter < SwaggerDefaultValues > ( ) ;
91
103
} ) ;
92
104
}
93
105
@@ -111,16 +123,16 @@ public static void ConfigurePasswordPolicy(this IServiceCollection services)
111
123
public static void ConfigureAuthentication ( this IServiceCollection services , IConfiguration configuration )
112
124
{
113
125
var key = Encoding . UTF8 . GetBytes ( configuration [ "ApplicationSettings:JWT_Secret" ] . ToString ( ) ) ;
114
- services . AddAuthentication ( x =>
126
+ services . AddAuthentication ( options =>
115
127
{
116
- x . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
117
- x . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
118
- x . DefaultScheme = JwtBearerDefaults . AuthenticationScheme ;
119
- } ) . AddJwtBearer ( x =>
128
+ options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
129
+ options . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
130
+ options . DefaultScheme = JwtBearerDefaults . AuthenticationScheme ;
131
+ } ) . AddJwtBearer ( options =>
120
132
{
121
- x . RequireHttpsMetadata = false ;
122
- x . SaveToken = false ;
123
- x . TokenValidationParameters = new TokenValidationParameters
133
+ options . RequireHttpsMetadata = false ; //only for DEV ENV
134
+ options . SaveToken = false ;
135
+ options . TokenValidationParameters = new TokenValidationParameters
124
136
{
125
137
ValidateIssuerSigningKey = true ,
126
138
IssuerSigningKey = new SymmetricSecurityKey ( key ) ,
@@ -130,5 +142,28 @@ public static void ConfigureAuthentication(this IServiceCollection services, ICo
130
142
} ;
131
143
} ) ;
132
144
}
145
+
146
+ public static void ConfigureApiVersioning ( this IServiceCollection services )
147
+ {
148
+ services . AddApiVersioning ( options =>
149
+ {
150
+ options . ReportApiVersions = true ;
151
+ options . DefaultApiVersion = new ApiVersion ( 1 , 0 ) ;
152
+ options . AssumeDefaultVersionWhenUnspecified = true ;
153
+ //options.ApiVersionReader = new MediaTypeApiVersionReader();
154
+ options . ApiVersionSelector = new CurrentImplementationApiVersionSelector ( options ) ;
155
+ } ) ;
156
+ }
157
+
158
+ public static void ConfigureVersionedApiExplorer ( this IServiceCollection services )
159
+ {
160
+ services . AddVersionedApiExplorer ( options =>
161
+ {
162
+ //The format of the version added to the route URL
163
+ options . GroupNameFormat = "'v'VVV" ;
164
+ //Tells swagger to replace the version in the controller route
165
+ options . SubstituteApiVersionInUrl = true ;
166
+ } ) ;
167
+ }
133
168
}
134
169
}
0 commit comments