1
1
using AutoMapper ;
2
+ using Microsoft . AspNetCore . Authentication . JwtBearer ;
2
3
using Microsoft . AspNetCore . Builder ;
4
+ using Microsoft . AspNetCore . Identity ;
3
5
using Microsoft . EntityFrameworkCore ;
4
6
using Microsoft . Extensions . Configuration ;
5
7
using Microsoft . Extensions . DependencyInjection ;
8
+ using Microsoft . IdentityModel . Tokens ;
6
9
using Supermarket . Domain . Services ;
7
10
using Supermarket . Domain . Services . Contracts ;
11
+ using Supermarket . Identity . Context ;
12
+ using Supermarket . Identity . Models ;
8
13
using Supermarket . Persistent . Context ;
9
14
using Supermarket . Persistent . Contracts ;
10
15
using Supermarket . Persistent . Repositories ;
11
16
using Swashbuckle . AspNetCore . Swagger ;
17
+ using System ;
18
+ using System . Text ;
12
19
13
20
namespace Supermarket . Extensions
14
21
{
@@ -33,6 +40,11 @@ public static void ConfigureIISIntegration(this IServiceCollection services)
33
40
34
41
public static void ConfigureMSSQLContext ( this IServiceCollection services , IConfiguration configuration )
35
42
{
43
+ services . AddDbContext < AuthenticationContext > ( options =>
44
+ {
45
+ options . UseSqlServer ( configuration . GetConnectionString ( "IdentityConnectionString" ) ) ;
46
+ } ) ;
47
+
36
48
services . AddDbContext < RepositoryContext > ( options =>
37
49
{
38
50
options . UseInMemoryDatabase ( "supermarket-api-in-memory" ) ;
@@ -78,5 +90,45 @@ public static void ConfigureSwagger(this IServiceCollection services)
78
90
} ) ;
79
91
} ) ;
80
92
}
93
+
94
+ public static void ConfigureIdentity ( this IServiceCollection services )
95
+ {
96
+ services . AddDefaultIdentity < ApplicationUser > ( ) . AddEntityFrameworkStores < AuthenticationContext > ( ) ;
97
+ }
98
+
99
+ public static void ConfigurePasswordPolicy ( this IServiceCollection services )
100
+ {
101
+ services . Configure < IdentityOptions > ( options =>
102
+ {
103
+ options . Password . RequireDigit = false ;
104
+ options . Password . RequireNonAlphanumeric = false ;
105
+ options . Password . RequireLowercase = false ;
106
+ options . Password . RequireUppercase = false ;
107
+ options . Password . RequiredLength = 4 ;
108
+ } ) ;
109
+ }
110
+
111
+ public static void ConfigureAuthentication ( this IServiceCollection services , IConfiguration configuration )
112
+ {
113
+ var key = Encoding . UTF8 . GetBytes ( configuration [ "ApplicationSettings:JWT_Secret" ] . ToString ( ) ) ;
114
+ services . AddAuthentication ( x =>
115
+ {
116
+ x . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
117
+ x . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
118
+ x . DefaultScheme = JwtBearerDefaults . AuthenticationScheme ;
119
+ } ) . AddJwtBearer ( x =>
120
+ {
121
+ x . RequireHttpsMetadata = false ;
122
+ x . SaveToken = false ;
123
+ x . TokenValidationParameters = new TokenValidationParameters
124
+ {
125
+ ValidateIssuerSigningKey = true ,
126
+ IssuerSigningKey = new SymmetricSecurityKey ( key ) ,
127
+ ValidateIssuer = false ,
128
+ ValidateAudience = false ,
129
+ ClockSkew = TimeSpan . Zero
130
+ } ;
131
+ } ) ;
132
+ }
81
133
}
82
134
}
0 commit comments