Created
February 2, 2023 04:07
-
-
Save Dark-Rex01/41672a084148f35a9cc9758609bc9a21 to your computer and use it in GitHub Desktop.
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
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