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软件透明挂!德州竞技... wepok软件透明挂!德州竞技联盟辅助,(wePOke)起初存在有挂(详细辅助2025新版教程)关于...
透视教程(德州微扑克辅助)微扑... 透视教程(德州微扑克辅助)微扑克真的有挂吗(详细辅助我来教教你)其实有挂(1)透视教程(德州微扑克辅...
aapoker有猫腻!德州ai... 《aapoker有猫腻!德州ai机器人辅助,(aa扑克)先前是有挂(详细透视细节方法)》 德州ai机...
透视软件(wepoke的确有挂... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...
aapoker挂!aapoke... aapoker挂!aapoker透视辅助,(wepoker)原来真的是有挂(详细透视技巧教程);科技...
透视攻略(wepoke黑科技)... 一、简介了解软件请加微:136704302是一款在线扑克游戏平台,玩家可以在平台上进行多人在线扑克游...
德州微扑克辅助!德州poker... 德州微扑克辅助!德州poker有外挂吗,(nzt德州)最初是有挂(详细辅助必备教程);最新版2024...
透视线上(wepower有外挂... 透视线上(wepower有外挂)wpk专用辅助程序(详细辅助总结教程)一向是有挂;一、有挂的是的,亲...
aapoker透视辅助!wep... 您好,aapoker透视辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视系统(wepoke的确有挂... 1、透视系统(wepoke的确有挂)wepoke辅助透(详细辅助解密教程)原生真的是有挂;详细教程。...