【问题标题】:Missing "aud" claim in access token访问令牌中缺少“aud”声明
【发布时间】:2020-11-05 20:53:00
【问题描述】:

由于未知原因,访问令牌中不存在“aud”声明(但它存在于 id 令牌中)。

将访问令牌发送到 API 后,我收到以下错误:

承载未通过身份验证。失败消息:IDX10214:Audience 验证失败。观众:“空”。不匹配: validationParameters.ValidAudience: 'productconfigurationapi' 或 validationParameters.ValidAudiences: 'null'。

我知道我可以关闭观众验证,然后一切正常,但我不明白为什么“aud”不是访问令牌的一部分。

这是我的 IS4 配置:

客户:

            new Client
            {
                ClientId = "Spa",
                AllowedGrantTypes = GrantTypes.Implicit,
                AllowAccessTokensViaBrowser = true,
                AlwaysSendClientClaims = true,
                AlwaysIncludeUserClaimsInIdToken = true,
                AccessTokenType = AccessTokenType.Jwt,
                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    "productconfigurationapi"
                },
                RequireConsent = false
            }

api 资源:

            new ApiResource("productconfigurationapi")
            {
                UserClaims =
                {
                    JwtClaimTypes.Audience
                }
            }

API 范围:

    return new List<ApiScope>
    {
        new ApiScope("productconfigurationapi")
    };

下面是 IS4 在其宿主应用程序中的配置方式:

        services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddConfigurationStore(options =>
            {
            })
            .AddOperationalStore(options =>
            {
            })
            .AddAspNetIdentity<IdentityUser>()
            .AddJwtBearerClientAuthentication();

【问题讨论】:

    标签: identityserver4


    【解决方案1】:

    您应该通过设置 Scopes 属性将 ApiScope 绑定到 ApiResource:

    var api = new ApiResource("productconfigurationapi")
    {
        UserClaims =
        {
            JwtClaimTypes.Audience
        },
        Scopes = new List<string>
        {
            "productconfigurationapi"
        },
    };
    

    【讨论】:

    • 似乎明确添加JwtClaimTypes.Audience UserClaim 是多余的。为客户端和 ApiResource 分配一个范围就足够了。
    猜你喜欢
    • 2018-09-12
    • 2018-02-07
    • 2020-01-13
    • 2019-02-25
    • 2021-07-25
    • 1970-01-01
    • 2020-02-13
    • 2015-05-16
    • 2016-11-15
    相关资源
    最近更新 更多