小编Ivá*_*lpo的帖子

.Net 6 的 System.IdentityModel.Tokens.Jwt 的自定义对象 JSON 序列化实现是否存在问题?

我们从 .Net Core (dotnet core) 3.1 迁移到 .Net 6。我们使用 System.IdentityModel.Tokens.Jwt 创建有效负载并使用该有效负载生成安全令牌。

我们的应用程序尚未从Newtonsoft.Json迁移到System.Text.Json,因为许多非标准属性序列化目前更倾向于前者。自定义声明值包含一个之前通过遵守Startup.cs配置中指定的有关 JSON 序列化的驼峰命名约定解析器正确序列化的对象。

我们从System.IdentityModel.Tokens.Jwt版本5.5.0升级到版本6.16.0,序列化的行为有所不同。

我们混合使用了 IdentityModel 众所周知的声明和自定义声明。自定义声明是唯一一个作为对象的声明,也是唯一一个具有这种行为方式的声明。所有其他声明都是原始类型,并按照指定和预期写入令牌。

这是不起作用的代码示例:

 var payload = new JwtPayload()
            {
                {JwtRegisteredClaimNames.Iss, issuer},
                {JwtRegisteredClaimNames.Iat, now},
                {JwtRegisteredClaimNames.Nbf, now},
                {JwtRegisteredClaimNames.Exp, exp},
                {JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")},
                {"role", user.UserType},
                {"customClaim", customClaimObjectInstance }
            };

var jwt = new JwtSecurityToken(_jwtHeader, payload);

/* Token below contains a valid base64 encoded JWT Token with
   the customClaim property containing pascal values that match
   the properties …
Run Code Online (Sandbox Code Playgroud)

jwt asp.net-core asp.net-core-webapi identitymodel .net-6.0

5
推荐指数
1
解决办法
1510
查看次数