首页 > 解决方案 > 多个 Azure AD 身份验证身份服务器 4

问题描述

我想知道是否可以使用 IdentityServer 4 设置一个 .NET Core 应用程序,该应用程序可以对多个 AzureAd 配置进行身份验证。

目前,您可以像这样添加 1 个 AzureAD 配置:

services.AddAuthentication()
            .AddAzureAD(options => Configuration.Bind("AzureAd", options));

但我希望能够使用 AzureAd 对来自多个组织的用户进行身份验证。如此不同的 TenantId ...等

这必须根据 UI 中选择的组织即时完成。

我怎样才能做到这一点?

标签: asp.net-coreazure-active-directoryidentityserver4

解决方案


您可以使用AddOpenIdConnect中间件:

services.AddAuthentication()
.AddOpenIdConnect("AADTenant1", "AADTenant1", options =>
{
    options.ClientId = "<app1>";
    options.Authority = "https://login.microsoftonline.com/<tenant1>/";             
    options.CallbackPath = "/signin-oidc-aadtenant1";            
    options.SaveTokens = true;                                 
    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

})
.AddOpenIdConnect("AADTenant2", "AADTenant2", options =>
{
    options.ClientId = "<app2>";
    options.Authority = "https://login.microsoftonline.com/<tenant2>/";             
    options.CallbackPath = "/signin-oidc-aadtenant2";
    options.SaveTokens = true;
    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

})

并触发您想要挑战的计划:

var callbackUrl = Url.Action("ExternalLoginCallback");

var props = new AuthenticationProperties
{
    RedirectUri = callbackUrl,
    Items =
    {
        { "scheme", provider },
        { "returnUrl", returnUrl }
    }
};

return Challenge(provider, props);

推荐阅读