Asp.net core 无效令牌-受众';空的';无效
我在ASP.NET Core 3.1项目上具有以下Identity Server 4配置:Asp.net core 无效令牌-受众';空的';无效,asp.net-core,jwt,identityserver4,asp.net-core-3.1,Asp.net Core,Jwt,Identityserver4,Asp.net Core 3.1,我在ASP.NET Core 3.1项目上具有以下Identity Server 4配置: services .AddIdentityServer(y => { y.Events.RaiseErrorEvents = true; y.Events.RaiseInformationEvents = true; y.Events.RaiseFailureEvents = true; y.Events.RaiseSuccessEvents = true;
services
.AddIdentityServer(y => {
y.Events.RaiseErrorEvents = true;
y.Events.RaiseInformationEvents = true;
y.Events.RaiseFailureEvents = true;
y.Events.RaiseSuccessEvents = true;
})
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.IdentityResources())
.AddInMemoryApiResources(Config.ApiResources())
.AddInMemoryApiScopes(Config.GetApiScopes())
.AddInMemoryClients(Config.Clients)
.AddProfileService<ProfileService>()
.AddAspNetIdentity<User>();
在API上,我有:
services
.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, x => {
x.ApiName = "api";
x.ApiSecret = "Secret"
x.Authority = Config.AuthorityUrl;
x.RequireHttpsMetadata = true;
x.EnableCaching = true;
x.CacheDuration = TimeSpan.FromMinutes(20);
});
我使用带OAuth2的失眠客户端调用了一个API端点:
我能够获取访问令牌,但调用API时出现错误:
Bearer error="invalid_token", error_description="The audience 'empty' is invalid"
Bearer error="invalid_token", error_description="The audience 'https://localhost:5000/resources' is invalid"
我使用JWT检查了访问令牌,得到了以下结果:
public static class Config {
public static List<ApiResource> ApiResources() {
return new List<ApiResource> {
new ApiResource("api", "API Resource")
};
}
public static List<ApiScope> ApiScopes() {
return new List<ApiScope> {
new ApiScope("api", "API Scope")
};
}
public static List<IdentityResource> IdentityResources() {
return new List<IdentityResource> {
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email()
};
}
public static List<Client> Clients() {
return new List<Client> {
new Client {
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets = { new Secret("Secret".Sha256()) }
AllowedScopes = {
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.OfflineAccess,
"api"
}
}
};
}
}
标题
{
"alg": "HS256",
"kid": "A1042705E52832C676596F36BB1898AB",
"typ": "at+jwt"
}
有效载荷
{
"nbf": 1598478410,
"exp": 1598482010,
"iss": "https://localhost:5000",
"client_id": "mvc",
"jti": "23525FF997FD4D71E13C786A7AD07B5D",
"iat": 1598478410,
"scope": [
"api"
]
}
缺少aud
。但是我需要它吗?阅读IS4文档后,我尝试添加:
.AddIdentityServer(y => {
y.EmitStaticAudienceClaim = true;
现在,访问令牌具有aud
字段,但其值不是api
。难道不是吗
"aud": "https://localhost:5000/resources"
调用API时,我现在得到错误:
Bearer error="invalid_token", error_description="The audience 'empty' is invalid"
Bearer error="invalid_token", error_description="The audience 'https://localhost:5000/resources' is invalid"
我阅读了文档和IS4示例,但找不到解决方案
我缺少什么?您需要的是将ApiScope与ApireSource连接起来,以使api成为所需的作用域。这个https://localhost:5000/resources 当作用域和api未“连接”时,aud claim是一般受众 在API定义中,您需要执行与本例类似的操作(查看下面的Scopes属性)
var invoiceApi=new apirource()
{
Name=“invoice”,//这是API的名称
Description=“这是发票Api资源描述”,
启用=真,
DisplayName=“发票API服务”,
Scopes=新列表{“shop.admin”、“shop.employee”},
};
有关所使用的通用资源范围的详细信息,请参见此
它说:
使用仅范围模型时,不会添加aud(观众)声明
因为这个概念不适用于令牌。如果你需要澳元
声明时,可以在选项上启用EmitStaticAudience设置。
这将以发卡机构名称/资源格式发出aud索赔。如果
您需要更多地控制aud索赔,请使用API资源
您可以尝试将EmitStaticAudience选项设置为false
您也可以忽略令牌中的两个受众。我按照您的建议进行了操作,但发生了一些奇怪的事情。现在,令牌中的
aud
是:“aud”:[“api”,“”]。既然现在api资源和作用域已连接,那么“”是否应该从aud
中消失?更新了我的答案,希望答案可以接受。/resources受众是4.x版或IdentityServer4版中的一个新事物,感谢您的澄清。我只是做了个记号作为答案。它真的帮助了我!