How to use multiple OpenIdConnectAuthenticationOptions in Azure active directory
azure, azure-active-directory, c#, oauth-2.0, openid-connect
Solution
You could try to use `AuthenticationType` . This property identifies this middleware in the pipeline and is used to refer to it for authentication operations . For example , you could define the configuration like :
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions("AADLogin")
{
AuthenticationMode = AuthenticationMode.Passive,
MetadataAddress = String.Format(aadInstance2, tenant2, SignUpSignInPolicyId),
ClientId = clientId2,
RedirectUri = redirectUri2,
PostLogoutRedirectUri = postLogoutRedirectUri,
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions("B2CLogin")
{
AuthenticationMode = AuthenticationMode.Passive,
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
});
Then depends on user selection , you could choose which one to use :
if ()
{
HttpContext.GetOwinContext()
.Authentication.Challenge(new AuthenticationProperties {RedirectUri = "/"},
"AADLogin");
}
else
{
HttpContext.GetOwinContext()
.Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
"B2CLogin");
}
Problem
In our project, we want to display two options (login as employee and login as customer). Based on the selection, we want to authenticate user either with Azure Active directory B2B or Azure B2C. I am able to set the Authentication mode to Passive and open login page after clicking on the link. It works well when single OpenIdConnectAuthenticationOptions is configured. But this does not work when I configure multiple OpenIdConnectAuthenticationOptions. ``` app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { AuthenticationMode = AuthenticationMode.Passive, MetadataAddress = String.Format(aadInstance2, tenant2, SignUpSignInPolicyId), ClientId = clientId2, RedirectUri = redirectUri2, PostLogoutRedirectUri = postLogoutRedirectUri, }); app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { AuthenticationMode = AuthenticationMode.Passive, ClientId = clientId, Authority = authority, PostLogoutRedirectUri = postLogoutRedirectUri, }); public void Redirect() { HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "https://localhost/WebApp1/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType); } ```