Skip to content

Commit 45d34e5

Browse files
committed
Update ServiceExtensions.cs
ConfigureIdentity ConfigurePasswordPolicy ConfigureAuthentication added
1 parent d3bfdc3 commit 45d34e5

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Supermarket/Extensions/ServiceExtensions.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
using AutoMapper;
2+
using Microsoft.AspNetCore.Authentication.JwtBearer;
23
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Identity;
35
using Microsoft.EntityFrameworkCore;
46
using Microsoft.Extensions.Configuration;
57
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.IdentityModel.Tokens;
69
using Supermarket.Domain.Services;
710
using Supermarket.Domain.Services.Contracts;
11+
using Supermarket.Identity.Context;
12+
using Supermarket.Identity.Models;
813
using Supermarket.Persistent.Context;
914
using Supermarket.Persistent.Contracts;
1015
using Supermarket.Persistent.Repositories;
1116
using Swashbuckle.AspNetCore.Swagger;
17+
using System;
18+
using System.Text;
1219

1320
namespace Supermarket.Extensions
1421
{
@@ -33,6 +40,11 @@ public static void ConfigureIISIntegration(this IServiceCollection services)
3340

3441
public static void ConfigureMSSQLContext(this IServiceCollection services, IConfiguration configuration)
3542
{
43+
services.AddDbContext<AuthenticationContext>(options =>
44+
{
45+
options.UseSqlServer(configuration.GetConnectionString("IdentityConnectionString"));
46+
});
47+
3648
services.AddDbContext<RepositoryContext>(options =>
3749
{
3850
options.UseInMemoryDatabase("supermarket-api-in-memory");
@@ -78,5 +90,45 @@ public static void ConfigureSwagger(this IServiceCollection services)
7890
});
7991
});
8092
}
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+
}
81133
}
82134
}

0 commit comments

Comments
 (0)