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(configuration.GetSection("JwtSettings")); //var vv = config.GetSection("ConnectionString:MTWorkHRConnectionString").Value; var vv = config.ConnectionStrings.MTWorkHRConnectionString;// ("ConnectionString:MTWorkHRConnectionString").Value; services.AddDbContext(options => options.UseSqlServer(config.ConnectionStrings.MTWorkHRConnectionString //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value )); services.AddIdentity().AddEntityFrameworkStores().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; } } }