Skip to content

Instantly share code, notes, and snippets.

@radzionc
Created October 7, 2020 13:52
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 radzionc/03ebf1db13678bbed6a6d4cece68d428 to your computer and use it in GitHub Desktop.
Save radzionc/03ebf1db13678bbed6a6d4cece68d428 to your computer and use it in GitHub Desktop.
public static string GenerateToken(string id, int days, string secret)
{
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, id)
}),
Expires = DateTime.UtcNow.AddDays(Convert.ToInt32(days)),
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(Convert.FromBase64String(secret)),
SecurityAlgorithms.HmacSha256Signature
)
};
return new JwtSecurityTokenHandler().WriteToken(
tokenHandler.CreateToken(tokenDescriptor)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment