在asp.net core 3.1中基于租户的注册身份验证方案


12

当前,我已经使用外部登录提供程序创建了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;
     });

预期的代码如下所示,

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);
 });


也许您可以在此线程中找到答案或解决方法。
Ruard van Elburg

@RuardvanElburg感谢您的答复,在您启动应用程序时,我们可以从租户建议的线程更新基于租户的已注册方案选项。之后我们无法更新它们。
Madhan kumar D

您是否希望每个租户具有不同的ClientId / ClientSecret设置?还是只想根据租户添加不同的方案?
柯克·拉金

@KirkLarkin具有各自租户的clientId / secret的不同方案。如下所示,if(tenant.hasMicrosoft){authentication.AddMicrosoftAccount(option => {option.ClientId =“ clientid”; option.ClientSecret =“ clientsecret”; option.SaveTokens = true;}); }
Madhan kumar D

这事有进一步更新吗?
Madhan kumar D

Answers:



0

由于需要在DI注册期间配置身份验证,因此通常必须在认证注册期间设置所有外部登录提供程序。

在该步骤中,您需要添加所有方案。方案具有固定的客户端ID /秘密,因此您需要使用支持所有客户端的所有外部登录提供程序凭据来引导IdentityServer。方案名称必须唯一。

例如,租户A可能有一个方案“ A_microsoft”,租户B可能有一个方案“ B_microsoft”,等等。

然后,在IdentityServer中调用方法时,可以引用那些身份验证方案。登录,挑战,退出等。

请注意,这将需要引导IdentityServer完整的租户集。根据您的方案,如果租户定期更新,则还需要定期重新启动IdentityServer才能知道新的身份验证方案。


如果这是一个问题,则可能可以在IdentityServer运行时以某种方式增强已注册的身份验证方案,但这并不容易。可能需要AspNetCore附带的身份验证中间件的更大的自定义实现。


0

您是说要添加对多个身份验证提供程序的支持吗?本文档已经指定了如何在配置服务中添加多个身份验证提供程序。无需再使用app.UseXXX自己来配置管道

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.