如何使用新的 Microsoft.IdentityModel.JsonWebTokens 创建 JwtSecurityToken?

问题描述 投票:0回答:1

因为 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.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);
c# jwt microsoft-identity-platform
1个回答
0
投票

这应该有效:

var data = Encoding.UTF8.GetBytes("SomeStringFromConfig1234 SomeStringFromConfig1234");
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(data);

var claims = new Dictionary<string, object>
{
    [ClaimTypes.Name] = "Testuser",
    [ClaimTypes.GroupSid] = "Tenant1",
    [ClaimTypes.Sid] = "3c545f1c-cc1b-4cd5-985b-8666886f985b"
};
var descriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor
{
    Issuer = "MyIssuer",
    Audience = "MyAudience",
    Claims = claims,
    IssuedAt = null,
    NotBefore = DateTime.UtcNow,
    Expires = DateTime.UtcNow.AddMinutes(120),
    SigningCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256Signature)
};

var handler = new Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler();
handler.SetDefaultTimesOnTokenCreation = false;
var tokenString = handler.CreateToken(descriptor);

然后如果比较输出字符串中的 JWT 负载:

{
  "aud": "MyAudience",
  "iss": "MyIssuer",
  "exp": 1709078400,
  "nbf": 1708992000,
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Testuser",
  "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid": "Tenant1",
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid": "3c545f1c-cc1b-4cd5-985b-8666886f985b"
}

以及代码中的 JWT 有效负载:

{
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Testuser",
  "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid": "Tenant1",
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid": "3c545f1c-cc1b-4cd5-985b-8666886f985b",
  "nbf": 1708992000,
  "exp": 1709078400,
  "iss": "MyIssuer",
  "aud": "MyAudience"
}

两个 JWT 有效负载具有相同的声明

© www.soinside.com 2019 - 2024. All rights reserved.