Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created December 28, 2019 23:04
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 xximjasonxx/2c3cacfeb051fcbb7c3098893390408a to your computer and use it in GitHub Desktop.
Save xximjasonxx/2c3cacfeb051fcbb7c3098893390408a to your computer and use it in GitHub Desktop.
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"],
_configuration["JwtAudience"],
claims,
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