由于https://www.nuget.org/packages/Microsoft.IdentityModel.JsonWebTokens是"具有额外功能的更新、更快的System.IdentityModel.Tokens.Jwt版本",但我没有找到一些关于如何切换到新的NuGet包的例子,我想问一下如何将以下代码(使用System.IdentityModel.Tokens.Jwt
和Microsoft.IdentityModel.Tokens
)转换为Microsoft.IdentityModel.JsonWebTokens
代码.当然,硬编码的值仅供参考.
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.Security算法rithms.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);