1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.IdentityModel.Tokens;
- using MTWorkHR.Core;
- using MTWorkHR.Identity.DBContext;
- using MTWorkHR.Identity.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MTWorkHR.Identity
- {
- public static class IdentityServiceRegistration
- {
- public static IServiceCollection AddIdentityServices(this IServiceCollection services, AppSettingsConfiguration config)
- {
- services.AddSingleton(config);
- // services.Configure<JwtSettings>(configuration.GetSection("JwtSettings"));
- //var vv = config.GetSection("ConnectionString:MTWorkHRConnectionString").Value;
- var vv = config.ConnectionStrings.MTWorkHRConnectionString;// ("ConnectionString:MTWorkHRConnectionString").Value;
- services.AddDbContext<HRIdentityDBContext>(options =>
- options.UseSqlServer(config.ConnectionStrings.MTWorkHRConnectionString
- //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value
- ));
- services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<HRIdentityDBContext>().AddDefaultTokenProviders();
-
- services.AddAuthentication(options => {
- options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; // "bearer"
- options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- }).AddJwtBearer(o=> o.TokenValidationParameters = new TokenValidationParameters
- {
- ValidateIssuerSigningKey = true,
- ValidateIssuer = true,
- ValidateAudience = true,
- ValidateLifetime = true,
- ClockSkew = TimeSpan.Zero,
- //ValidIssuer = configuration["JwtSettings:Issuer"],
- //ValidAudience = configuration["JwtSettings:Audience"],
- ValidIssuer = config.JwtSettings.Issuer,
- ValidAudience = config.JwtSettings.Audience,
- IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.JwtSettings.SecretKey))
- }) ;
- return services;
- }
- }
- }
|