访问令牌中缺少 "aud" 声明

Missing "aud" claim in access token

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

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

Bearer was not authenticated. Failure message: IDX10214: Audience validation failed. Audiences: 'empty'. Did not match: validationParameters.ValidAudience: 'productconfigurationapi' or 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();

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

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