Last active
July 22, 2019 14:58
-
-
Save ntakouris/9fc5881e8d5983680568ee9fc1188d6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async Task<AuthenticationResult> GenerateAuthenticationResultForUserAsync(IdentityUser user) | |
{ | |
var tokenHandler = new JwtSecurityTokenHandler(); | |
var key = Encoding.ASCII.GetBytes(_jwtSettings.Secret); | |
var claims = new List<Claim> | |
{ | |
new Claim(JwtRegisteredClaimNames.Sub, user.Email), | |
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | |
new Claim(JwtRegisteredClaimNames.Email, user.Email), | |
new Claim("id", user.Id) | |
}; | |
var userClaims = await _userManager.GetClaimsAsync(user); | |
claims.AddRange(userClaims); | |
var tokenDescriptor = new SecurityTokenDescriptor | |
{ | |
Subject = new ClaimsIdentity(claims), | |
Expires = DateTime.UtcNow.Add(_jwtSettings.TokenLifetime), | |
SigningCredentials = | |
new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) | |
}; | |
var token = tokenHandler.CreateToken(tokenDescriptor); | |
return new AuthenticationResult | |
{ | |
Success = true, | |
Token = tokenHandler.WriteToken(token) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment