【问题标题】:How to use identityserver3 in asp.net core 2.0 webapi to validate token from Identityserver3 server如何在 asp.net core 2.0 webapi 中使用 identityserver3 来验证来自 Identityserver3 服务器的令牌
【发布时间】:2025-11-22 07:35:01
【问题描述】:

我有一个使用 IdentityServer3 来发布令牌的身份服务器。

我正在创建一个 asp.net core 2.0 api 客户端。

如何在 ASP.Net Core 2.0 api 应用中验证 Identityserver3 颁发的令牌?

我尝试安装 Identityserver3.AccessTokenValidation.AspNetCore,但收到错误提示它与核心不兼容。

谁能帮我怎么做?

谢谢

【问题讨论】:

    标签: identityserver3 asp.net-core-webapi


    【解决方案1】:

    使用.Net Core 2,您可以使用IdentityServer4.AccessTokenValidation 来验证IdentityServer3 令牌,只需确保在ConfigureServices 方法中添加这一行

    options.LegacyAudienceValidation = true;
    

    ConfigureServices 应该如下所示:

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvcCore(options =>
                {
                    // IS3 does not include the api name/audience - hence the extra scope check
                    options.Filters.Add(new AuthorizeFilter(ScopePolicy.Create("api")));
                })
                    .AddAuthorization();
    
                services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
                    .AddIdentityServerAuthentication(options =>
                    {
                        options.Authority = "http://localhost:5002";
                        options.RequireHttpsMetadata = false;
    
                        options.ApiName = "api";
                        options.ApiSecret = "secret";
    
                        // this is only needed because IS3 does not include the API name in the JWT audience list
                        // so we disable UseIdentityServerAuthentication JWT audience check and rely upon
                        // scope validation to ensure we're only accepting tokens for the right API
                        options.LegacyAudienceValidation = true;
                    });
            }
    

    更多信息可以参考link

    【讨论】:

      最近更新 更多