Asp.net Core 2 使用 Identity Server 4 启用多租户

Gzi*_*ani 6 c# multi-tenant asp.net-core identityserver4

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

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

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

Gzi*_*ani 6

我解决了这个问题。

在启动类的保护 API 级别,我有以下配置:

services.AddAuthentication("Bearer")
        .AddIdentityServerAuthentication(options =>
        {
            options.Authority = "https://shared-domain-for-every-tenant/";
            options.RequireHttpsMetadata = true;
            options.ApiName = "atb_api";
        });
Run Code Online (Sandbox Code Playgroud)

神奇发生在 IDP 级别 (IdentityServer4),在配置 IdentityServer 时,我添加了选项 IssuerUri,如下所示:

services.AddIdentityServer(options => {
            options.IssuerUri = "https://shared-domain-for-every-tenant/";
        })..AddDeveloperSigningCredential() ...other configurations ...
Run Code Online (Sandbox Code Playgroud)

当我导航到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",
    ...
  }
Run Code Online (Sandbox Code Playgroud)

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

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