【问题标题】:Why doesn't IdentityServer4 ApiResource work with JwtBearerOption.Audience?为什么 IdentityServer4 ApiResource 不能与 JwtBearerOption.Audience 一起使用?
【发布时间】:2020-12-07 07:48:09
【问题描述】:

我在 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”,则受众将失效

IdentityServer4 documentation about related to my question.

【问题讨论】:

    标签: c# asp.net-core-webapi identityserver4


    【解决方案1】:

    要让 MyApi 进入受众列表,您需要像在 IdentityServer4 (v4.0x) 中一样定义 ApiScope

    查看articles了解更多详情

    【讨论】:

    • 如果是这样,ApiScope(s) 也是 Audience 吗?
    • 是的,ApiScopes 变成了受众,localhost:5001/resources 似乎是始终存在的标准受众?
    • 虽然 IS4 文档没有说明如何操作,但您给了我一个想法,我也在 ApiResource 配置中添加了范围。
    • 有一位神,他的名字是@RonaldAbellano。我一直在搜索我能找到的所有东西,直到我遇到你的知识宝藏才将范围添加到 ApiResource!文件没有这么说!!!我已经在这里待了几天了,你刚刚救了我。谢谢谢谢谢谢。
    猜你喜欢
    • 2019-01-07
    • 2018-02-02
    • 1970-01-01
    • 2018-03-09
    • 2021-06-14
    • 2012-10-09
    • 2020-03-18
    • 2017-11-21
    • 2019-04-11
    相关资源
    最近更新 更多