如何在 ASP.NET Core 2.0 WebAPI 中使用 IdentityServer3 验证来自 IdentityServer3 服务器的令牌

4
我有一个使用IdentityServer3来发布令牌的身份验证服务器。
我正在创建一个asp.net core 2.0 api客户端。
如何在ASP.Net Core 2.0 api应用程序中验证Identityserver3发出的令牌?
我尝试安装Identityserver3.AccessTokenValidation.AspNetCore,但是得到了错误消息,说它与core不兼容。
有人能帮我怎么做吗?
谢谢。
1个回答

3

使用 .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;
                });
        }

如需更多信息,请参考此链接

该链接涉及IT技术相关内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接