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

相关内容

热门资讯

一分钟了解!wepoker透视... 一分钟了解!wepoker透视脚本免费下载(辅助挂)都是真的有挂(发现有挂辅助教程)亲,关键说明,透...
盘点几款!wepoker公共底... 盘点几款!wepoker公共底牌(辅助挂)总是真的有挂(有挂详情辅助技巧)1、让任何用户在无需安装教...
六分钟了解!wejoker透视... 六分钟了解!wejoker透视方法(辅助挂)其实存在有挂(有挂规律辅助教程)暗藏猫腻,小编详细说明破...
安装程序教程!aapoker辅... 安装程序教程!aapoker辅助软件合法吗(辅助挂)果然是真的有挂(有挂秘诀辅助技巧)1、操作简单,...
一分钟了解!hardrock作... 一分钟了解!hardrock作弊(辅助挂)好像真的有挂(有挂教程辅助器)1、超多福利:超高返利,海量...
推荐十款!德普之星透视辅助软件... 推荐十款!德普之星透视辅助软件下载(辅助挂)本来真的是有挂(有挂头条辅助教程)1. 选牌创建新账号,...
科技通报!wepoker高级辅... 科技通报!wepoker高级辅助(辅助挂)一贯真的是有挂(有挂教程辅助工具)1、全新机制【ai辅助工...
玩家必看分享!wepoker安... 玩家必看分享!wepoker安装教程(辅助挂)确实是真的有挂(有挂秘笈辅助教程)暗藏猫腻,小编详细说...
热点讨论!impoker辅助(... 热点讨论!impoker辅助(辅助挂)切实是真的有挂(确实有挂辅助攻略)1、在插件功能辅助器技巧中,...
分享给玩家!拱趴大菠萝挂(辅助... 分享给玩家!拱趴大菠萝挂(辅助挂)总是有挂(有挂实锤辅助app)1、上手简单,内置详细流程视频教学,...