Mad*_*r D 13 authentication multi-tenant asp.net-core identityserver4 saaskit
目前,我已经创建了一个 Identity server 4 Web 应用程序,其中包含具有默认客户端 ID 和机密的外部登录提供程序。但我的目标是基于租户注册 Azure、Google、Facebook 等身份验证提供程序。
我使用了SaasKit多租户程序集,在这里我尝试了app.usepertenant()中间件。但是UseGoogleAuthentication()方法已经过时了,所以我无法使用这个 usepertenant 中间件实现多租户身份验证。
当前代码,
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddMicrosoftAccount(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
Run Code Online (Sandbox Code Playgroud)
预期的代码如下,
var authentication = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
if (tenant.hasMicrosoft)
{
authentication.AddMicrosoftAccount(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
}
if (tenant.hasGoogle)
{
authentication.AddGoogle(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
}
authentication.AddCookie( options =>
{
options.SlidingExpiration = true;
options.ExpireTimeSpan = new TimeSpan(7, 0, 0, 0);
});
Run Code Online (Sandbox Code Playgroud)
由于在 DI 注册期间需要配置身份验证,因此您通常必须在身份验证注册期间设置所有外部登录提供程序。
在此步骤中,您需要添加所有方案。方案具有固定的客户端 ID/秘密,因此您需要使用所有外部登录提供程序凭据来引导 IdentityServer,以支持所有客户端。方案名称必须是唯一的。
例如,租户 A 可能有方案“A_microsoft”,租户 B 可能有方案“B_microsoft”,等等。
然后,您可以在调用 IdentityServer 中的方法时引用这些身份验证方案。登录、挑战、退出等。
请注意,这将需要引导 IdentityServer 一组完整的租户。根据您的场景,如果定期更新租户,还需要定期重新启动 IdentityServer 才能了解新的身份验证方案。
如果这是一个问题,您可能可以在 IdentityServer 运行时以某种方式增强注册的身份验证方案,但这并不容易。它可能需要 AspNetCore 附带的身份验证中间件的更大的自定义实现。
| 归档时间: |
|
| 查看次数: |
1657 次 |
| 最近记录: |