使用Identity Server 4实现Asp.net Core 2多租户功能

8

我可以帮您进行翻译。以下是需要翻译的内容:

我有一个使用多个绑定(auth.company1.com和auth.company2.com)托管的IDP(Identity Server 4)。我还有一个被该IDP保护的API。因此,为了访问API,我需要从IDP获得访问令牌。这是在API级别的启动类中配置的,如下所示:

     services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "https://auth.company1.com/";
                options.RequireHttpsMetadata = true;
                options.ApiName = "atb_api";
            });

如何动态配置options.Authority,以使其允许来自多个域名的授权 https://auth.company1.com/https://auth.company2.com/ ?

1个回答

7
我解决了这个问题。 在启动类的保护API级别上,我有以下配置:
services.AddAuthentication("Bearer")
        .AddIdentityServerAuthentication(options =>
        {
            options.Authority = "https://shared-domain-for-every-tenant/";
            options.RequireHttpsMetadata = true;
            options.ApiName = "atb_api";
        });

魔法发生在 IDP 层面(IdentityServer4),在配置 IdentityServer 时,我添加了 IssuerUri 选项,如下所示:
services.AddIdentityServer(options => {
            options.IssuerUri = "https://shared-domain-for-every-tenant/";
        })..AddDeveloperSigningCredential() ...other configurations ...

当我访问 https://auth.company1.com/.well-known/openid-configuration 时, 返回的文档如下:
  {
    "issuer": "https://shared-domain-for-every-tenant/",
    "jwks_uri": "https://auth.company1.com/.well-known/openid-configuration/jwks",
    "authorization_endpoint": "https://auth.company1.com/connect/authorize",
    "token_endpoint": "https://auth.company1.com/connect/token",
    "userinfo_endpoint": "https://auth.company1.com/connect/userinfo",
    ...
  }

请注意,issure是一个静态url,而所有其他端点都是特定于发出请求的租户。这使API能够验证访问令牌,并为每个租户拥有不同的端点(我需要这样做以显示每个租户的不同登录屏幕)。
希望它能帮助到某个人 :)

在使用AddJwtBearer()扩展时,您可以指定自定义的Issuer验证方法并在那里进行验证。查找config.TokenValidationParameters.IssuerValidator。 根据项目文档,提供AddIdentityServerAuthentication的包似乎不再维护。 - nod

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