Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2019 23:04
What would you like to do?
public string CreateToken(User user)
if (user == null)
throw new ArgumentException(nameof(user));
var claims = new[]
new Claim(ClaimTypes.NameIdentifier, user.Username)
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtKey"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);
var token = new JwtSecurityToken(_configuration["JwtIssuer"],
expires: DateTime.Now.AddMonths(1),
signingCredentials: creds);
return new JwtSecurityTokenHandler().WriteToken(token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment