[英]Invalid Token - The audience 'empty' is invalid
我在 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;
})
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.IdentityResources())
.AddInMemoryApiResources(Config.ApiResources())
.AddInMemoryApiScopes(Config.GetApiScopes())
.AddInMemoryClients(Config.Clients)
.AddProfileService<ProfileService>()
.AddAspNetIdentity<User>();
Config
如下:
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"
}
}
};
}
}
在 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 的 Insomnia 客戶端調用了一個 API 端點:
我能夠獲得訪問令牌,但是在調用 API 時出現錯誤:
Bearer error="invalid_token", error_description="The audience 'empty' is invalid"
我使用 JWT 檢查了訪問令牌,得到以下信息:
標題
{
"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 'https://localhost:5000/resources' is invalid"
我閱讀了文檔和 IS4 示例,但找不到解決方案。
我錯過了什么?
您需要的是將 ApiScope 與 APIRespource 連接起來,以使 api 成為所需的范圍。 當范圍和 api 未“連接”時,https://localhost:5000/resources aud 聲明是通用受眾。
在您的 API 定義中,您需要執行此示例中的操作(查看下面的 Scopes 屬性)
var invoiceApi = new ApiResource()
{
Name = "invoice", //This is the name of the API
Description = "This is the invoice Api-resource description",
Enabled = true,
DisplayName = "Invoice API Service",
Scopes = new List<string> { "shop.admin", "shop.employee" },
};
有關使用的通用資源范圍的詳細信息,請參閱此鏈接。
它說:
使用僅作用域模型時,不會向令牌添加任何 aud(受眾)聲明,因為此概念不適用。 如果您需要 aud 聲明,您可以在選項上啟用 EmitStaticAudience 設置。 這將以 issuer_name/resources 格式發出 aud 聲明。 如果您需要更多地控制 aud 聲明,請使用 API 資源。
您可以嘗試將EmitStaticAudience選項設置為false 。
您也可以忽略令牌中的兩個受眾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.