为什么 IdentityServer4 ApiResource 不能与 JwtBearerOption.Audience 一起使用?
Why doesn't IdentityServer4 ApiResource work with JwtBearerOption.Audience?
我在 IdentityServer 中有这个配置:
public static IEnumerable<ApiResource> ApiResources =>
new ApiResource[]
{
new ApiResource
{
Name = "MyApi"
}
};
以及 ASP.NET Core web API 上的这个 jwt 配置:
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
//identity server
options.Authority = "https://localhost:5001";
//access token recepient
options.Audience = "https://localhost:5001/resources";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidateLifetime = true,
};
});
我预计 Web API 身份验证不会接受来自 IdentityServer 的令牌,因为 Web API JwtBearerOption.Audience 不等于“MyApi”。但在我的配置中,仅当受众设置为“https://localhost:5001/resources”时才会验证受众,如果我将其设置为“MyApi”
,则会失效
要让 MyApi 进入受众列表,您需要像在 IdentityServer4 (v4.0x) 中那样定义一个 ApiScope
查看此articles了解更多详情
我在 IdentityServer 中有这个配置:
public static IEnumerable<ApiResource> ApiResources =>
new ApiResource[]
{
new ApiResource
{
Name = "MyApi"
}
};
以及 ASP.NET Core web API 上的这个 jwt 配置:
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
//identity server
options.Authority = "https://localhost:5001";
//access token recepient
options.Audience = "https://localhost:5001/resources";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = true,
ValidateLifetime = true,
};
});
我预计 Web API 身份验证不会接受来自 IdentityServer 的令牌,因为 Web API JwtBearerOption.Audience 不等于“MyApi”。但在我的配置中,仅当受众设置为“https://localhost:5001/resources”时才会验证受众,如果我将其设置为“MyApi”
,则会失效要让 MyApi 进入受众列表,您需要像在 IdentityServer4 (v4.0x) 中那样定义一个 ApiScope
查看此articles了解更多详情