Created
July 22, 2019 09:37
-
-
Save ntakouris/db6322a63713b04c1b397f35b9acd909 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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment