Skip to content

Instantly share code, notes, and snippets.

@Mirch
Created March 24, 2019 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mirch/7415884ff1af1cf60161fa723b48b167 to your computer and use it in GitHub Desktop.
Save Mirch/7415884ff1af1cf60161fa723b48b167 to your computer and use it in GitHub Desktop.
private string BuildToken(string email)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.UTF8.GetBytes(_authenticationSettings.JwtKey);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, email)
}),
Expires = DateTime.UtcNow.AddDays(7),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var jwtToken = tokenHandler.CreateToken(tokenDescriptor);
var token = tokenHandler.WriteToken(jwtToken);
return token;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment