public async Task <IActionResult> Login(ApplicationUserLoginModel model) { var user = await this.userManager.FindByNameAsync(model.UserName); IdentityOptions options = new IdentityOptions(); if (user != null && await this.userManager.CheckPasswordAsync(user, model.Password)) { var role = await this.userManager.GetRolesAsync(user); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim("UserID", user.Id.ToString()), new Claim(options.ClaimsIdentity.RoleClaimType, role.FirstOrDefault()) }), Expires = DateTime.UtcNow.AddDays(1), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes("1234567890123456")), SecurityAlgorithms.HmacSha256Signature) }; var tokenHandler = new JwtSecurityTokenHandler(); var securityToken = tokenHandler.CreateToken(tokenDescriptor); var token = tokenHandler.WriteToken(securityToken); return(Ok(new { token })); } else { return(this.BadRequest(new { message = "Username or password is incorrect." })); } }
public async Task <IActionResult> Login(ApplicationUserLoginModel model) { var user = await _userManager.FindByEmailAsync(model.Email); if (user != null && await _userManager.CheckPasswordAsync(user, model.Password)) { List <Claim> claims = new List <Claim>(); claims.Add(new Claim("Id", user.Id.ToString())); claims.Add(new Claim("Email", user.Email)); var roles = await _userManager.GetRolesAsync(user); IdentityOptions identityOptions = new IdentityOptions(); foreach (string role in roles) { claims.Add(new Claim(identityOptions.ClaimsIdentity.RoleClaimType, role)); } var securityTokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims.ToArray()), Expires = DateTime.UtcNow.AddDays(1), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Startup.Configuration["JWTkey"].ToString())), SecurityAlgorithms.HmacSha256Signature) }; var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var securityToken = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor); var token = jwtSecurityTokenHandler.WriteToken(securityToken); return(Ok(new { token })); } else { return(BadRequest(new { message = "Invalid login attempt." })); } }
//POST : /api/ApplicationUser/Login public async Task <IActionResult> UserLogin(ApplicationUserLoginModel model) { var applicationUser = await _userManager.FindByNameAsync(model.UserName); if (applicationUser != null && await _userManager.CheckPasswordAsync(applicationUser, model.Password)) { // Get user assigned role var role = await _userManager.GetRolesAsync(applicationUser); IdentityOptions _options = new IdentityOptions(); if (!ApplicationUserExists(applicationUser.Id)) { var applicationUserRole = new ApplicationUserRole() { ID = applicationUser.Id, UserName = applicationUser.UserName, FirstName = applicationUser.FirstName, LastName = applicationUser.LastName, Email = applicationUser.Email, Role = role.FirstOrDefault() }; await _context.ApplicationUserRoles.AddAsync(applicationUserRole); await _context.SaveChangesAsync(); } var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim("UserID", applicationUser.Id.ToString()), new Claim(_options.ClaimsIdentity.RoleClaimType, role.FirstOrDefault()) }), Expires = DateTime.UtcNow.AddMinutes(20), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_applicationSettings.JWT_Token)), SecurityAlgorithms.HmacSha256Signature) }; var tokenHandler = new JwtSecurityTokenHandler(); var securityToken = tokenHandler.CreateToken(tokenDescriptor); var token = tokenHandler.WriteToken(securityToken); return(Ok(new { token })); } else { return(BadRequest(new { message = "Username or password is incorrect." })); } }