出于我未知的原因,访问令牌中不存在“ aud”声明(尽管它在id令牌中存在)。
将访问令牌发送到API后,出现以下错误:
承载者未通过身份验证。失败消息:IDX10214:受众 验证失败。观众:“空”。不匹配: validationParameters.ValidAudience:“ productconfigurationapi”或 validationParameters.ValidAudiences:“空”。
我知道我可以关闭受众验证,然后一切正常,但是我不明白为什么“ 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();
答案 0 :(得分:5)
您应通过设置Scopes属性将ApiScope绑定到ApiResource:
var api = new ApiResource("productconfigurationapi")
{
UserClaims =
{
JwtClaimTypes.Audience
},
Scopes = new List<string>
{
"productconfigurationapi"
},
};