Mig*_*ura 4 jwt asp.net-core 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;
})
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.IdentityResources())
.AddInMemoryApiResources(Config.ApiResources())
.AddInMemoryApiScopes(Config.GetApiScopes())
.AddInMemoryClients(Config.Clients)
.AddProfileService<ProfileService>()
.AddAspNetIdentity<User>();
Run Code Online (Sandbox Code Playgroud)
该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"
}
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
在 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);
});
Run Code Online (Sandbox Code Playgroud)
我使用带有 OAuth2 的 Insomnia 客户端调用了一个 API 端点:
我能够获得访问令牌,但是在调用 API 时出现错误:
Bearer error="invalid_token", error_description="The audience 'empty' is invalid"
Run Code Online (Sandbox Code Playgroud)
我使用 JWT 检查了访问令牌,得到以下信息:
标题
{
"alg": "HS256",
"kid": "A1042705E52832C676596F36BB1898AB",
"typ": "at+jwt"
}
Run Code Online (Sandbox Code Playgroud)
有效载荷
{
"nbf": 1598478410,
"exp": 1598482010,
"iss": "https://localhost:5000",
"client_id": "mvc",
"jti": "23525FF997FD4D71E13C786A7AD07B5D",
"iat": 1598478410,
"scope": [
"api"
]
}
Run Code Online (Sandbox Code Playgroud)
的aud缺失。但我需要吗?阅读 IS4 文档后,我尝试添加:
.AddIdentityServer(y => {
y.EmitStaticAudienceClaim = true;
Run Code Online (Sandbox Code Playgroud)
现在访问令牌有aud字段但它的值不是api。不应该吗?
"aud": "https://localhost:5000/resources"
Run Code Online (Sandbox Code Playgroud)
调用 API 时,我现在收到错误消息:
Bearer error="invalid_token", error_description="The audience 'https://localhost:5000/resources' is invalid"
Run Code Online (Sandbox Code Playgroud)
我阅读了文档和 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" },
};
Run Code Online (Sandbox Code Playgroud)
有关使用的通用资源范围的详细信息,请参阅此链接。
它说:
使用仅作用域模型时,不会向令牌添加任何 aud(受众)声明,因为此概念不适用。如果您需要 aud 声明,您可以在选项上启用 EmitStaticAudience 设置。这将以 issuer_name/resources 格式发出 aud 声明。如果您需要更多地控制 aud 声明,请使用 API 资源。
您可以尝试将EmitStaticAudience选项设置为false。
您也可以忽略令牌中的两个受众。
| 归档时间: |
|
| 查看次数: |
3955 次 |
| 最近记录: |