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

推荐答案

这应该是可行的:

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.Security算法rithms.HmacSha256Signature)
};

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

然后,如果从输出字符串中比较JWT payload:

{
  "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有效载荷都有相同的索赔

Csharp相关问答推荐

为什么我在PuppeteerSharp中运行StealthPlugin时会出现错误?

当Visual Studio处于升级管理模式时,无法安装Torch运行时

IComponition.获取IReadOnlyCollection的返回默认属性值

使用变量子根名称在C#中重新初始化SON文件

将委托传递到serviceccollection c#web API

在. net毛伊岛窗口的深度链接已经创建""

是否可以使用EF—Core进行临时部分更新?

在C#中,DirectoryEntry返回空AuditRules集合,即使审计规则确实存在

Polly v8—使用PredicateBuilder重试特定的状态代码

C#EF Core 8.0表现与预期不符

从ASP.NET Core中的枚举字段填充 Select 选项时,将默认的第一个选项添加为 Select 元素

取决于您的数据量的多个嵌套循环

是否可以将Collectionview中的数组与ObservableCollection绑定?

VS 2022与VS 2019:如何/为什么创建额外的任务?

源代码生成器:CS8795分部方法';Class1.GetS2(字符串)';必须有实现部分,因为它有可访问性修饰符?

如何从Entity Framework Core中填充ListIInterface

在同一个捕获中可以有多种类型的异常吗?

删除MudRadio时,MudRadioGroup未 Select 正确的MudRadio

将两个for循环更改为一条LINQ语句

如果图表S批注包含使用LINQ的具有特定名称的批注,我如何签入C#