Advertisement
otkalce

Generate JWT

Mar 9th, 2023
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 KB | Software | 0 0
  1.         // Get secret key bytes
  2.         var tokenKey = Encoding.UTF8.GetBytes("12345678901234567890123456789012");
  3.  
  4.         // Create a token descriptor (represents a token, kind of a "template" for token)
  5.         var tokenDescriptor = new SecurityTokenDescriptor
  6.         {
  7.             Expires = DateTime.UtcNow.AddMinutes(10),
  8.             SigningCredentials = new SigningCredentials(
  9.                 new SymmetricSecurityKey(tokenKey),
  10.                 SecurityAlgorithms.HmacSha256Signature)
  11.         };
  12.  
  13.         // Create token using that descriptor, serialize it and return it
  14.         var tokenHandler = new JwtSecurityTokenHandler();
  15.         var token = tokenHandler.CreateToken(tokenDescriptor);
  16.         var serializedToken = tokenHandler.WriteToken(token);
Tags: jwt-token
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement