Since https://www.nuget.org/packages/Microsoft.IdentityModel.JsonWebTokens is a 'newer, faster version of System.IdentityModel.Tokens.Jwt that has additional functionality', but I didn't find some example on how to switch to the new NuGet package, I wanted to ask how to convert the following code (Which uses System.IdentityModel.Tokens.Jwt and Microsoft.IdentityModel.Tokens) to Microsoft.IdentityModel.JsonWebTokens code. The hardcoded values are just for reference, of course.
var data = Encoding.UTF8.GetBytes("SomeStringFromConfig1234");
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(data);
var claims = new List<System.Security.Claims.Claim>
{
new System.Security.Claims.Claim(ClaimTypes.Name, "Testuser"),
new System.Security.Claims.Claim(ClaimTypes.GroupSid, "Tenant1"),
new System.Security.Claims.Claim(ClaimTypes.Sid, "3c545f1c-cc1b-4cd5-985b-8666886f985b")
});
var algorithms = Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256Signature;
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, algorithms));
var token = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(
"MyIssuer",
"MyAudience",
claims,
expires: DateTime.UtcNow.AddMinutes(120),
signingCredentials: credentials;
var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler();
var tokenString = tokenHandler.WriteToken(token);
This should work:
Then if compare JWT payload from the output string:
with the JWT payload from original code:
both JWT payloads have the same claims