Skip to content

Instantly share code, notes, and snippets.

@Dark-Rex01
Created February 2, 2023 04:07
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 Dark-Rex01/41672a084148f35a9cc9758609bc9a21 to your computer and use it in GitHub Desktop.
Save Dark-Rex01/41672a084148f35a9cc9758609bc9a21 to your computer and use it in GitHub Desktop.
public string GenerateToken(Authentication user)
{
// generate token that is valid for 7 days
List<Claim> claims = new List<Claim>
{
new Claim("email", user.Email),
new Claim("role", user.UserRole.ToString()),
new Claim("name", user.UserName),
new Claim("id", user.AuthId.ToString())
};
var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Secret").Value));
var cred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
claims: claims,
expires: DateTime.Now.AddDays(1),
signingCredentials: cred
);
var jwt = new JwtSecurityTokenHandler().WriteToken(token);
return jwt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment