首页 > 解决方案 > 使用多个 IDServer 保护单个 api 资源

问题描述

所以我有一个 .Net Core web api,我们称它为“CMS”,它目前由 IdentityServer4 服务器作为 api 资源保护。我已将 ID4 服务器配置为具有 MyIDP 的 IDP 声明。

出于商业原因,我需要为客户提供他们自己的 IdentityServer,但他们也希望让他们的用户访问相同的 api "CMS" 。

这可能吗?在我的 CMS api 的 StartUp.cs 中,它目前看起来像这样

services.AddAuthentication("Bearer")
    .AddIdentityServerAuthentication(options =>
                {
                    options.Authority = "http://www.idserver1.com";   
                    options.RequireHttpsMetadata = true; 
                    options.ApiName = "cmsapi"; 
                });

因此,为了增加对另一个 id 服务器的保护,我假设我可以复制AddAuthentication但将方案名称从 Bearer 更改为其他名称,但这似乎是错误的?

我认为这应该是可能的原因是我已经能够以这种方式将多个外部提供程序添加到我的 Web 应用程序中。但这是针对 s 登录流程,而不是针对 api。

如果这是可能的,我该怎么做?

标签: identityserver4

解决方案


这可以很简单地实现。假设您想为每个客户颁发一个单独的子域:auth0.yourdomain.comauth1.yourdomain.com并且您希望 api 资源尊重来自这些身份提供者中的任何一个的令牌。

假设签名密钥相同,您可以在身份服务器端配置共享颁发者 uri Startup.cs->ConfigureServices(...)

        var builder = services.AddIdentityServer(options => {
                              options.IssuerUri = "auth.yourdomain.com";
                              })
                 ...

然后在 api 方面,您可以尊重单个颁发者 uri,而无需复制身份验证方案:

services.AddAuthentication("Bearer")
    .AddIdentityServerAuthentication(options =>
                {
                    options.Authority = "auth.yourdomain.com";   
                    options.RequireHttpsMetadata = true; 
                    options.ApiName = "cmsapi"; 
                });    

我不记得的一件事是是否为颁发者 uri 推断了请求方案 (http/https),​​因此您可能还需要指定它 ( https:\\auth.yourdomain.com)。除此之外,就您的客户而言,这种实现应该是非常无缝的。


推荐阅读