c# - 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/的权限?
解决方案
我解决了这个问题。
在启动类的保护 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 验证访问令牌,并且每个租户也有不同的端点(我需要这个来为每个租户显示不同的登录屏幕)。
希望它可以帮助那里的人:)
推荐阅读
- python - 如何将多个分类列转换为在 PySpark 中保持共享值的整数?
- android - 如何在底部导航栏添加一行
- discord - Discord.py 投票命令没有响应
- python - 使用 asyncio 协程并行运行函数?
- javascript - TypeError:无法读取未定义的属性“执行”(Discord.js)
- javascript - AG-Grid agNumberColumnFilter 不适用于 valueFormatter 中的计算值
- javascript - 拉取firebase文档获取多余数据
- python - 导入csv,每行列数不一致,原始标题使用熊猫
- javascript - NextJS:动态创建的 html 节点不被 css 重绘
- c - 如何从文件中填充和打印结构中的数组?