好让我迷茫 . 我有一个Angular应用程序,可以使用IdentityServer4登录 . Angular应用程序是clientId web . 当我解码jwt时,aud只有web .
我创建了一个api资源 - api .
现在,当我尝试使用访问令牌调用我的.NET Core Api时,我得到了受众验证失败错误 . 基本上说它期待网络,并且它作为 Spectator 得到了api .
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ApiName = "api";
options.SaveToken = true;
});
如果我将ApiName更改为web,它可以工作 . jwt中列出的范围都是我所期望的 . 为了进一步增加我的困惑,当我查看dbo.PersistedGrants时,数据中列出的受众是
"Audiences": [
"http://localhost:5000/resources",
"api"
]
客户:
new Client
{
ClientId = "web",
ClientName = "Web",
AccessTokenType = AccessTokenType.Jwt,
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
AlwaysIncludeUserClaimsInIdToken = true,
RedirectUris = { "http://localhost:5005" },
PostLogoutRedirectUris = { "http://localhost:5005/logout" },
AllowedCorsOrigins = { "http://localhost:5005", "https://localhost:5005" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"role",
"api"
},
RequireConsent = false
}
API
new ApiResource("api", "API")
IdentityResource
return new[]
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email(),
new IdentityResource("role", new[] { JwtClaimTypes.Role })
{
Required = true
}
};
我希望jwt对 Spectator 有相同的 Value .
我的理解是错误的还是错过了配置IdentityServer4的东西?
1 回答
确保在您的身份验证请求中包含
scope: "api"
. 如果您不对IdentityServer(或任何OIDC提供商)进行身份验证,则不会在您的jwt受众属性中获取api资源名称 .您的请求应如下所示:
http://my.identiyserverBaseUrl/connect/authorize?client_id= D&redirect_uri =&response_type = token& scope=api &state = [OPTIONAL_STATE]
如果您正在使用npm oidc-client,那么您的配置对象可能如下所示:
注意 scope . 您可以要求多个范围,例如
scope: 'api openid email'
. 如果您要求openid范围,则必须在响应类型中包含id_token
.Here's the documentation和here for the JavaScript client setup