访问令牌中缺少“aud”声明

sky*_*ner 5 identityserver4

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

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

Bearer 未经过身份验证。失败消息:IDX10214:受众验证失败。观众:“空”。不匹配: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
            }
Run Code Online (Sandbox Code Playgroud)

api资源:

            new ApiResource("productconfigurationapi")
            {
                UserClaims =
                {
                    JwtClaimTypes.Audience
                }
            }
Run Code Online (Sandbox Code Playgroud)

API 范围:

    return new List<ApiScope>
    {
        new ApiScope("productconfigurationapi")
    };
Run Code Online (Sandbox Code Playgroud)

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

        services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddConfigurationStore(options =>
            {
            })
            .AddOperationalStore(options =>
            {
            })
            .AddAspNetIdentity<IdentityUser>()
            .AddJwtBearerClientAuthentication();
Run Code Online (Sandbox Code Playgroud)

And*_*ndy 10

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

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

  • 看起来明确添加 `JwtClaimTypes.Audience` UserClaim 是多余的。为客户端和 ApiResource 分配一个范围就足够了。 (2认同)