Created
February 19, 2021 16:19
-
-
Save evan-boissonnot/3175e4c9df9b88b62d569470658cf4b1 to your computer and use it in GitHub Desktop.
Generate token JWT C#
This file contains 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 string GenerateJwtToken(IdentityUser user) | |
{ | |
// Now its ime to define the jwt token which will be responsible of creating our tokens | |
var jwtTokenHandler = new JwtSecurityTokenHandler(); | |
// We get our secret from the appsettings | |
var key = Encoding.ASCII.GetBytes(_jwtConfig.Secret); | |
// we define our token descriptor | |
// We need to utilise claims which are properties in our token which gives information about the token | |
// which belong to the specific user who it belongs to | |
// so it could contain their id, name, email the good part is that these information | |
// are generated by our server and identity framework which is valid and trusted | |
var tokenDescriptor = new SecurityTokenDescriptor | |
{ | |
Subject = new ClaimsIdentity(new [] | |
{ | |
new Claim("Id", user.Id), | |
new Claim(JwtRegisteredClaimNames.Sub, user.Email), | |
new Claim(JwtRegisteredClaimNames.Email, user.Email), | |
// the JTI is used for our refresh token which we will be convering in the next video | |
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) | |
}), | |
// the life span of the token needs to be shorter and utilise refresh token to keep the user signedin | |
// but since this is a demo app we can extend it to fit our current need | |
Expires = DateTime.UtcNow.AddHours(6), | |
// here we are adding the encryption alogorithim information which will be used to decrypt our token | |
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature) | |
}; | |
var token = jwtTokenHandler.CreateToken(tokenDescriptor); | |
var jwtToken = jwtTokenHandler.WriteToken(token); | |
return jwtToken; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment