public async Task <AuthenticationModel> GetTokenAsync(TokenRequestModel model) { var authenticationModel = new AuthenticationModel(); var user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { authenticationModel.IsAuthenticated = false; authenticationModel.Message = $"No Accounts Registered with {model.Email}."; return(authenticationModel); } if (await _userManager.CheckPasswordAsync(user, model.Password)) { authenticationModel.IsAuthenticated = true; JwtSecurityToken jwtSecurityToken = await CreateJwtToken(user); authenticationModel.Token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken); authenticationModel.Email = user.Email; authenticationModel.UserName = user.UserName; var rolesList = await _userManager.GetRolesAsync(user).ConfigureAwait(false); authenticationModel.Roles = rolesList.ToList(); if (user.RefreshTokens.Any(a => a.IsActive)) { var activeRefreshToken = user.RefreshTokens.Where(a => a.IsActive == true).FirstOrDefault(); authenticationModel.RefreshToken = activeRefreshToken.Token; authenticationModel.RefreshTokenExpiration = activeRefreshToken.Expires; } else { var refreshToken = RefreshToken.CreateRefreshToken(); authenticationModel.RefreshToken = refreshToken.Token; authenticationModel.RefreshTokenExpiration = refreshToken.Expires; user.RefreshTokens.Add(refreshToken); _context.Update(user); _context.SaveChanges(); } return(authenticationModel); } authenticationModel.IsAuthenticated = false; authenticationModel.Message = $"Incorrect Credentials for user {user.Email}."; return(authenticationModel); }
public async Task <AuthenticationModel> RefreshTokenAsync(string token) { var authenticationModel = new AuthenticationModel(); var user = _context.Users.SingleOrDefault(u => u.RefreshTokens.Any(t => t.Token == token)); if (user == null) { authenticationModel.IsAuthenticated = false; authenticationModel.Message = $"Token did not match any users."; return(authenticationModel); } var refreshToken = user.RefreshTokens.Single(x => x.Token == token); if (!refreshToken.IsActive) { authenticationModel.IsAuthenticated = false; authenticationModel.Message = $"Token Not Active."; return(authenticationModel); } //Revoke Current Refresh Token refreshToken.Revoked = DateTime.UtcNow; //Generate new Refresh Token and save to Database var newRefreshToken = RefreshToken.CreateRefreshToken(); user.RefreshTokens.Add(newRefreshToken); _context.Update(user); _context.SaveChanges(); //Generates new jwt authenticationModel.IsAuthenticated = true; JwtSecurityToken jwtSecurityToken = await CreateJwtToken(user); authenticationModel.Token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken); authenticationModel.Email = user.Email; authenticationModel.UserName = user.UserName; var rolesList = await _userManager.GetRolesAsync(user).ConfigureAwait(false); authenticationModel.Roles = rolesList.ToList(); authenticationModel.RefreshToken = newRefreshToken.Token; authenticationModel.RefreshTokenExpiration = newRefreshToken.Expires; return(authenticationModel); }
public RefreshToken ToEntity() => RefreshToken.CreateRefreshToken(UserId, Token, CreatedAt, ExpiryTime, RevokedAt);