ASP.NETMVC中的指标值类型显示为http://www.w3.org/2001/XMLSchema#string,而非JsonArray。
创始人
2024-09-19 09:01:04
0

可以通过自定义 ClaimSerializer 来解决。在 Startup.cs 中添加下面的代码:

services
    .AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = YourSecurityKey,

            ValidateIssuer = true,
            ValidIssuer = "your-issuer",

            ValidateAudience = true,
            ValidAudience = "your-audience",

            ValidateLifetime = true,

            ClockSkew = TimeSpan.Zero
        };

        options.ClaimActions.MapJsonKey(ClaimTypes.Role, ClaimTypes.Role, "string");
        options.ClaimActions.MapJsonKey(ClaimTypes.Name, ClaimTypes.Name, "string");

        // Add this line to change the claim value type to JsonArray
        options.ClaimActions.Add(new JsonArrayClaimAction("your-claim", "value"));
    });

需要注意的是,在上面的代码中,我们通过添加 JsonArrayClaimAction 将指定的 Claim 的值类型更改为 JsonArray。您需要将 your-claimvalue 替换为您自己的 Claim 和值。

假设您的 JWT Token 包含 your-claim:["value1","value2"],那么现在您可以通过以下代码来获取该 Claim:

var claim = User.FindFirst("your-claim");
var values = JsonConvert.DeserializeObject(claim.Value);

相关内容

热门资讯

透视详情"wepok... 透视详情"wepoker辅助真的假的"好像真的有辅助插件(哔哩哔哩)1、wepoker辅助真的假的破...
透视解谜"wepok... 透视解谜"wepoker好友局透视"好像真的有辅助挂(哔哩哔哩)运wepoker好友局透视辅助工具,...
透视推荐"wepok... 透视推荐"wepoker透视方法"切实有辅助工具(哔哩哔哩)1、玩家可以在wepoker透视方法线上...
透视解谜"wepok... 透视解谜"wepoker透视有用吗"一直真的是有辅助工具(哔哩哔哩)1、下载好wepoker透视有用...
透视专业"wpk辅助... 透视专业"wpk辅助器"都是存在有辅助攻略(哔哩哔哩)1)wpk辅助器辅助插件:进一步探索wpk辅助...
透视科普"wepok... 透视科普"wepoker轻量版辅助"果然存在有辅助插件(哔哩哔哩)1、进入到wepoker轻量版辅助...
透视揭幕"hhpok... 透视揭幕"hhpoker开挂教程"确实是真的辅助教程(哔哩哔哩)1、进入到hhpoker开挂教程是否...
透视了解"约局吧德州... 透视了解"约局吧德州透视"一直有辅助方法(哔哩哔哩)1、许多玩家不知道约局吧德州透视辅助怎么退出观战...
透视解谜"wepok... 透视解谜"wepoker透视app下载"一直是有辅助技巧(哔哩哔哩)1、下载好wepoker透视ap...
透视科普"xpoke... 透视科普"xpoker辅助器"都是是有辅助工具(哔哩哔哩)1、玩家可以在xpoker辅助器透视最简单...