public ActionResult Registration([FromForm] Customer customer) { Customer findCustomer = _mapper.Map <Customer>(_service.GetCustomer(customer.Login, customer.Password)); if (findCustomer != null) { var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Email, findCustomer.Email), new Claim(ClaimsIdentity.DefaultRoleClaimType, findCustomer.Role) }; JwtSecurityToken validationToken = new JwtSecurityToken( issuer: JWT_Options.ISSUER, audience: findCustomer.Login, claims: claims, notBefore: DateTime.Now, expires: DateTime.Now.AddMinutes(JWT_Options.LIFETIME), signingCredentials: new SigningCredentials(JWT_Options.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256) ); var JWT = new JwtSecurityTokenHandler().WriteToken(validationToken); var response = new { access_token = JWT, userName = findCustomer.Login, userRole = "Customer" }; return(Ok(response)); } return(BadRequest("Пользователь с данным логином уже присудствует!")); }
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddAuthentication("OAuth_2.0") .AddJwtBearer("JWT", config => { config.RequireHttpsMetadata = false; config.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = JWT_Options.ISSUER, ValidateAudience = true, ValidAudience = JWT_Options.AUDIENCE, ValidateIssuerSigningKey = true, IssuerSigningKey = JWT_Options.GetSymmetricSecurityKey() }; }); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); var mapperConfig = new MapperConfiguration(cfg => { ConfigMapper.Init(cfg); MapperConfig.Init(cfg); }); mapperConfig.CompileMappings(); var mapper = new Mapper(mapperConfig); ConfigServices.BuildConfig(services, Configuration.GetConnectionString("DefaultConnection")); services.AddSingleton <IMapper>(mapperConfig.CreateMapper()); services.AddScoped <ICarService, CarService>(); services.AddScoped <ICarClassService, CarClassService>(); services.AddScoped <ICompanyService, CompanyService>(); services.AddScoped <IOrderService, OrderService>(); services.AddScoped <IImageService, ImageService>(); services.AddScoped <ICustomerService, CustomerService>(); }
public ActionResult Authorize([FromForm] UserBase userBase) { var user = _mapper.Map <UserDefault>(_customerService.GetCustomer(userBase.Login, userBase.Password)); if (user != null) { var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Email, user.Email), new Claim(ClaimsIdentity.DefaultNameClaimType, user.Login), new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role) }; JwtSecurityToken validationToken = new JwtSecurityToken( issuer: JWT_Options.ISSUER, audience: JWT_Options.AUDIENCE, claims: claims, notBefore: DateTime.Now, expires: DateTime.Now.AddMinutes(JWT_Options.LIFETIME), signingCredentials: new SigningCredentials(JWT_Options.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256) ); var JWT = new JwtSecurityTokenHandler().WriteToken(validationToken); var response = new { access_token = JWT, userName = user.Login, userRole = user.Role }; return(Ok(response)); } return(BadRequest("Неверный логин или пароль!")); }