identityserver3 - 如何在 asp.net core 2.0 web api 中使用 identityserver3 来验证来自 Identityserver3 服务器的 token

标签 identityserver3 asp.net-core-webapi

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

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

如何在 ASP.Net Core 2.0 api 应用程序中验证 Identityserver3 发出的 token ?

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

任何人都可以帮助我如何做到这一点?

谢谢

最佳答案

.Net Core 2您可以使用 IdentityServer4.AccessTokenValidation要验证 IdentityServer3 token ,只需确保在 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

关于identityserver3 - 如何在 asp.net core 2.0 web api 中使用 identityserver3 来验证来自 Identityserver3 服务器的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47891185/

相关文章:

c# - OWIN Google 身份验证 - 如何获取 id_token?

c# - 如何将 AzureAD 和 AzureADBearer 添加到 asp.net core 2.2 web api

c# - IDX20803 : Unable to obtain configuration from: 'https://localhost/IdentityServer/Core/.well-known/openid-configuration'

c# - IdentityServer3 的 ASP.NET 身份插件 UI (IdentityManager) 在部署到 Azure (WebApp) 后不起作用

c# - 获取 IdentityServer3 以使用现有用户 sql 数据库

c# - 为 WebAPI 中的 Enum 描述实现一个 TypeConverter

rest - DTO 和 API 资源有什么区别

powershell - 本地服务器上的 Invoke-RestMethod 身份验证失败

c# - 当使用 JSON 中的重复键反序列化字典时,如何强制抛出异常?

authentication - 操作的 AuthorizeAttribute