private void SetRoles(UserForAuthentication user, List <Claim> claims) { var userRoles = _context.UserRoles.Include("Role").Where(x => x.UserId == user.Id); foreach (var role in user.Roles) { claims.Add(new Claim(ClaimTypes.Role, role)); } }
public async Task <IActionResult> GetToken([FromBody] UserForAuthentication userForAuth) { if (!await _authenticationManager.ValidateUserAsync(userForAuth)) { return(Unauthorized()); } var token = await _authenticationManager.CreateTokenAsync(); return(Ok(new { Token = token })); }
private IEnumerable <Claim> GetClaims(UserForAuthentication user) { var claims = new List <Claim>() { new Claim(JwtRegisteredClaimNames.Email, user.EmailAddress), new Claim(JwtRegisteredClaimNames.NameId, user.Id.ToString()), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; SetRoles(user, claims); return(claims); }
private string GenerateJSONWebToken(UserForAuthentication userInfo) { var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.Value.Key)); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken(_jwtOptions.Value.Issuer, _jwtOptions.Value.Issuer, GetClaims(userInfo), expires: DateTime.Now.AddHours(_appSettings.Value.TokenTimeOutInHours), signingCredentials: credentials); return(new JwtSecurityTokenHandler().WriteToken(token)); }
private UserForAuthentication AuthenticateUser(UserForAuthentication login) { UserForAuthentication user = null; var userAccount = _context.Users.FirstOrDefault(u => u.EmailAddress == login.EmailAddress && u.PasswordHash == login.PasswordHash); if (userAccount != null) { var userRoles = _context.UserRoles.Include("Role").Where(r => r.UserId == userAccount.Id); user = new UserForAuthentication { EmailAddress = userAccount.EmailAddress, Id = userAccount.Id }; user.Roles = userRoles.Select(x => x.Role.RoleName).ToArray(); } return(user); }
public IActionResult Login([FromBody] UserForAuthentication login) { IActionResult response = Unauthorized("Invalid username or password."); var user = AuthenticateUser(login); if (user != null) { var tokenString = GenerateJSONWebToken(user); response = new JsonResult(new { user.Id, JsonToken = tokenString, Roles = user.Roles }); } return(response); }
public async Task <bool> ValidateUserAsync(UserForAuthentication userForAuth) { _user = await _userManager.FindByNameAsync(userForAuth.UserName); return(_user != null && await _userManager.CheckPasswordAsync(_user, userForAuth.Password)); }