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

相关内容

热门资讯

盘点一款!微乐自建房辅助工具神... 盘点一款!微乐自建房辅助工具神器,微乐自建房脚本免费入口(开挂自建房)教程1、任何微乐自建房辅助工具...
分享实测!微乐自建房辅助工具,... 分享实测!微乐自建房辅助工具,微乐小程序免费黑科技(开挂自建房)软件1、首先打开微乐自建房辅助工具辅...
推荐一款!微乐微信自建房辅助工... 推荐一款!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)插件1、任何微乐微信自建房辅...
实测揭晓!微乐自建房辅助工具插... 实测揭晓!微乐自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)攻略;所有人都在同一条线...
盘点一款!微乐小程序麻将自建房... 盘点一款!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)教程1、微乐小...
推荐十款!微乐自建房辅助工具脚... 推荐十款!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)教程1、微乐自建房辅助工具脚本辅...
盘点几款!微乐自建房辅助工具免... 盘点几款!微乐自建房辅助工具免费app,微乐自建房脚本免费入口(开挂自建房)app1.微乐自建房辅助...
最新技巧!微信卡五星辅助器,微... 最新技巧!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法微信卡五星辅助器透视方法中分为三...
实操分享!微乐自建房辅助工具免... 实操分享!微乐自建房辅助工具免费app,微乐小程序免费黑科技(开挂自建房)软件1、进入游戏-大厅左侧...
技术分享!微信卡五星辅助器,微... 技术分享!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)工具1)微信卡五星辅助器有没有挂:进...