首页 > 解决方案 > Asp.net Core 2 使用 Identity Server 4 启用多租户

问题描述

我有一个 IDP(身份服务器 4)托管多个绑定:auth.company1.com 和 auth.company2.com 我还有一个受 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/的权限?

标签: c#asp.net-coreidentityserver4multi-tenant

解决方案


我解决了这个问题。

在启动类的保护 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",
    ...
  }

请注意,问题是一个静态 url,而所有其他端点都特定于发出请求的租户。这允许 API 验证访问令牌,并且每个租户也有不同的端点(我需要这个来为每个租户显示不同的登录屏幕)。

希望它可以帮助那里的人:)


推荐阅读