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);

相关内容

热门资讯

关于透视!xpoker辅助神器... 关于透视!xpoker辅助神器!总是是真的有辅助攻略(有挂技巧)-哔哩哔哩1)xpoker辅助神器免...
分享透视!cloudpoker... 分享透视!cloudpoker作必弊!确实有辅助脚本(有挂秘笈)-哔哩哔哩1、许多玩家不知道clou...
揭露透视!竞技联盟透视!切实是... 揭露透视!竞技联盟透视!切实是真的有辅助软件(有挂技巧)-哔哩哔哩1、该软件可以轻松地帮助玩家将竞技...
关于透视!红龙poker有辅助... 关于透视!红龙poker有辅助吗!切实一直都是有辅助攻略(有挂秘籍)-哔哩哔哩1、下载好红龙poke...
详情透视!智星菠萝透视!本来一... 详情透视!智星菠萝透视!本来一直都是有辅助app(今日头条)-哔哩哔哩1、下载好智星菠萝透视脚本下载...
科普透视!pokernow辅助... 科普透视!pokernow辅助工具!原来存在有辅助方法(有挂工具)-哔哩哔哩1、很好的工具软件,可以...
曝光透视!pokemmo免费脚... 曝光透视!pokemmo免费脚本!都是是有辅助app(有挂方式)-哔哩哔哩1、pokemmo免费脚本...
了解透视!werplan透视挂... 了解透视!werplan透视挂!好像有辅助神器(有挂神器)-哔哩哔哩1、这是跨平台的werplan透...
专业透视!pokemmo修改器... 专业透视!pokemmo修改器手机版!真是是有辅助插件(真是有挂)-哔哩哔哩1、下载好pokemmo...
分享透视!epoker透视底牌... 分享透视!epoker透视底牌!切实真的有辅助插件(有挂秘诀)-哔哩哔哩1、实时epoker透视底牌...