Asp.net core 在Identityserver4.NET核心中启用多个AzureAd
我正在尝试在一个身份服务器中启用多个AzureAD的登录功能 这是必要的,因为多个租户需要能够通过1个identityserver登录到他们自己的AzureAD。这些设置将动态进行 对于本例,我对AuthenticationBuilder进行了扩展,它允许传递多个AzureAd并将它们添加到AuthenticationBuilder中Asp.net core 在Identityserver4.NET核心中启用多个AzureAd,asp.net-core,azure-active-directory,identityserver4,Asp.net Core,Azure Active Directory,Identityserver4,我正在尝试在一个身份服务器中启用多个AzureAD的登录功能 这是必要的,因为多个租户需要能够通过1个identityserver登录到他们自己的AzureAD。这些设置将动态进行 对于本例,我对AuthenticationBuilder进行了扩展,它允许传递多个AzureAd并将它们添加到AuthenticationBuilder中 //startup.cs Dictionary<string, string> azure1 = new Dictionary<
//startup.cs
Dictionary<string, string> azure1 = new Dictionary<string, string>();
azure1.Add("name", "azure1");
azure1.Add("clientid", <azure1id>);
azure1.Add("tenantid", <azure1tenant>);
Dictionary<string, string> azure2 = new Dictionary<string, string>();
azure2.Add("name", "azure2");
azure2.Add("clientid", <azure2id>);
azure2.Add("tenantid", <azure2tenant>);
Dictionary<string, string>[] ids = { azure1, azure2 };
services.AddAuthentication()
.AddCookie()
.AddAzureAdAuthentications(ids)
//AuthenticationBuilderExtension
public static AuthenticationBuilder AddAzureAdAuthentications(this AuthenticationBuilder builder, Dictionary<string, string>[] azureAds)
{
string name = "";
string clientid = "";
string tenantid = "";
foreach (Dictionary<string, string> azuread in azureAds)
{
name = azuread.GetValueOrDefault("name");
clientid = azuread.GetValueOrDefault("clientid");
tenantid = azuread.GetValueOrDefault("tenantid");
builder.AddOpenIdConnect(name, name, options =>
{
options.Authority = $"https://login.microsoftonline.com/{tenantid}";
options.ClientId = clientid;
options.ResponseType = OpenIdConnectResponseType.IdToken;
});
}
return builder;
}
每个处理程序至少需要一个唯一的
回调路径
-还有其他用于注销和post重定向的回调-如果您使用这些功能,也必须设置它们
此外,身份验证方案必须是唯一的。我编辑了这篇文章,其中包含针对IdentityServer 3上的用户的answerNote的实施结果-上述答案是100%有效的,除了AuthenticationType必须是唯一的,以代替AuthenticationScheme
foreach (Dictionary<string, string> azuread in ids)
{
string name = azuread.GetValueOrDefault("name");
string clientid = azuread.GetValueOrDefault("clientid");
string tenantid = azuread.GetValueOrDefault("tenantid");
services.AddAuthentication(name).AddCookie($"Cookie{name}").AddOpenIdConnect(name, name, options =>
{
options.SignInScheme = $"Cookie{name}";
options.SignOutScheme = $"Cookie1{name}";
options.CallbackPath = $"/signin-{name}";
options.Authority = $"https://login.microsoftonline.com/{tenantid}";
options.ClientId = clientid;
options.ResponseType = OpenIdConnectResponseType.IdToken;
});
}
services.AddAuthentication().AddCookie($"Cookie{name}")
.AddOpenIdConnect(name, name, options =>
{
options.CallbackPath = $"/signin-{name}";
options.Authority = $"https://login.microsoftonline.com/{tenantid}";
options.ClientId = clientid;
options.ResponseType = OpenIdConnectResponseType.IdToken;
});