public LoginResponseDto Login(UserReadDto user) { ClaimsIdentity identity = GetIdentity(user); DateTime now = DateTime.UtcNow; JwtSecurityToken jwt = new JwtSecurityToken( issuer: _authConfigsManager.GetIssuer(), audience: _authConfigsManager.GetAudience(), notBefore: now, claims: identity.Claims, expires: now.Add(TimeSpan.FromMinutes(_authConfigsManager.GetLifetime())), signingCredentials: new SigningCredentials(_authConfigsManager.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256)); string encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); return(new LoginResponseDto { AccessToken = encodedJwt }); }
public async Task <LoginResponseDto> Login(string email, string password) { ClaimsIdentity identity = await GetIdentity(email, password); DateTime now = DateTime.UtcNow; JwtSecurityToken jwt = new JwtSecurityToken( issuer: _options.Value.Issuer, audience: _options.Value.Audience, notBefore: now, claims: identity.Claims, expires: now.Add(TimeSpan.FromMinutes(_options.Value.Lifetime)), signingCredentials: new SigningCredentials(_authConfigsManager.GetSymmetricSecurityKey(_options.Value.Key), SecurityAlgorithms.HmacSha256)); string encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); return(new LoginResponseDto { AccessToken = encodedJwt, Email = identity.Name }); }
public void ConfigureServices(IServiceCollection services) { services.AddDbContext <UserContext>(opt => opt.UseSqlServer (Configuration.GetConnectionString("UserManagerApiConnection")).EnableSensitiveDataLogging()); services.AddControllers().AddNewtonsoftJson(s => { s.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); services.AddScoped <AuthConfigsManager>(); ServiceProvider serviceProvider = services.BuildServiceProvider(); AuthConfigsManager authConfigsManager = serviceProvider.GetService <AuthConfigsManager>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = authConfigsManager.GetIssuer(), ValidateAudience = true, ValidAudience = authConfigsManager.GetAudience(), ValidateLifetime = true, IssuerSigningKey = authConfigsManager.GetSymmetricSecurityKey(), ValidateIssuerSigningKey = true, }; options.Events = new JwtBearerEvents { OnMessageReceived = context => { var accessToken = context.Request.Query["access_token"]; var path = context.HttpContext.Request.Path; if (!string.IsNullOrEmpty(accessToken) && (path.StartsWithSegments("/chat"))) { context.Token = accessToken; } return(Task.CompletedTask); } }; }); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddScoped <IUserRepository, UserRepository>(); services.AddScoped <IUserService, UserService>(); services.AddScoped <IAuthService, AuthService>(); services.AddSignalR(); services.AddCors(options => options.AddPolicy("CorsPolicy", builder => { builder.AllowAnyMethod().AllowAnyHeader() .WithOrigins("http://127.0.0.1:5500/websocket.html"); //.AllowCredentials();//AllowAnyOrigin(); })); }
public void ConfigureServices(IServiceCollection services) { services.AddDbContext <LibraryContext>(options => options.UseSqlServer ( Configuration.GetConnectionString("DefaultConnection") ).EnableSensitiveDataLogging() ); services.AddCors(options => { options.AddPolicy("CORS", builder => { builder .WithOrigins( Configuration["App:CorsOrigins"] .Split(",", StringSplitOptions.RemoveEmptyEntries) .ToArray() ) .SetIsOriginAllowedToAllowWildcardSubdomains() .WithExposedHeaders("Location", "Upload-Offset", "Upload-Length") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); services.AddControllers(options => options.Filters.Add(new ApiExceptionFilter())) .ConfigureApiBehaviorOptions(options => { options.SuppressModelStateInvalidFilter = true; }) .AddNewtonsoftJson(s => { s.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); s.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; }); services.Configure <ApiOptions>(Configuration.GetSection("Jwt")); services.AddScoped <AuthConfigsManager>(); ServiceProvider serviceProvider = services.BuildServiceProvider(); AuthConfigsManager authConfigsManager = serviceProvider.GetService <AuthConfigsManager>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = Configuration.GetSection("Jwt").GetValue <string>("issuer"), ValidateAudience = true, ValidAudience = Configuration.GetSection("Jwt").GetValue <string>("audience"), ValidateLifetime = true, IssuerSigningKey = authConfigsManager.GetSymmetricSecurityKey( Configuration.GetSection("Jwt").GetValue <string>("key")), ValidateIssuerSigningKey = true, }; }); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "Library Management API" }); }); services.AddSignalR(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddScoped <IAuthService, AuthService>(); services.AddScoped <IAuthorRepo, AuthorRepo>(); services.AddScoped <IAuthorService, AuthorService>(); services.AddScoped <IGenreRepo, GenreRepo>(); services.AddScoped <IGenreService, GenreService>(); services.AddScoped <IBookRepo, BookRepo>(); services.AddScoped <IBookService, BookService>(); services.AddScoped <IUserRepo, UserRepo>(); services.AddScoped <IUserService, UserService>(); services.AddScoped <IOrderRepo, OrderRepo>(); services.AddScoped <IOrderService, OrderService>(); services.AddScoped <INotificationsService, NotificationsService>(); }