-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] Is it possible to add AzureAD *and* AzureAD B2C authentication to the same web app? #11972
Comments
@jmprieur is this doable? |
@blowdart : I don't think it is. I need to try out |
I think this is a known issue in 2.2. We fixed it in 3.0 |
The workaround is to do the options configuration yourself |
@javiercn I thought about that but couldn't figure out a couple of things. First off I imagine I'd register a single implementation of And the issue I hit was, the classes that do this for the framework ( |
I had this same issue and tried figuring out some way to make them play nice together but was not able to. Doing the configuration manually did not seem practical as it would require essentially rewriting the module due to internal only configuration classes (eg. |
Okay spoke to soon, seem to have it working as ugly as it is... In // hack to just grab preconfigured options
var dummyCollection = new ServiceCollection();
dummyCollection.AddAuthentication("dummy").AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));
var sp = dummyCollection.BuildServiceProvider();
var openIdSnapshot = sp.GetService<IOptionsSnapshot<OpenIdConnectOptions>>();
var b2cOpenIdOpts = openIdSnapshot.Get(AzureADB2CDefaults.OpenIdScheme);
var cookieSnapshot = sp.GetService<IOptionsSnapshot<CookieAuthenticationOptions>>();
var b2cCookieOpts = cookieSnapshot.Get(AzureADB2CDefaults.CookieScheme);
services.AddAuthentication("AADorB2C")
.AddPolicyScheme("AADorB2C", "Selects AAD or B2C", o =>
{
o.ForwardDefaultSelector = ctx =>
{
// app logic to select if you want to use AAD or B2C
};
})
.AddAzureAD(options => Configuration.Bind("AzureAd", options))
.AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));
// configure the B2C options here by copying the pertinent values from above
services.Configure<OpenIdConnectOptions>(AzureADB2CDefaults.OpenIdScheme, c => {
c.ClientId = b2cOpenIdOpts.ClientId;
c.ClientSecret = b2cOpenIdOpts.ClientSecret;
c.Authority = b2cOpenIdOpts.Authority;
c.CallbackPath = b2cOpenIdOpts.CallbackPath;
c.SignedOutCallbackPath = b2cOpenIdOpts.SignedOutCallbackPath;
c.SignInScheme = b2cOpenIdOpts.SignInScheme;
c.TokenValidationParameters = b2cOpenIdOpts.TokenValidationParameters;
c.Events = b2cOpenIdOpts.Events;
// add other non-default options you may want
});
// do same for cookies (although less important, just sets the redirect URLs) |
I have this issue too, in 3.0 & 3.1. Between
The options configuration for both OpenID schemes gets run on both the AAD and B2C OIDC options configurations. This is fine for the respective scheme, but fails for the other (see table below) because the scheme doesn't exist in the mappings (which are added to the OpenIDMappings dictionary on the AzureAD(B2C)SchemeOptions by the auth builder extension), so the options for that specific scheme can't be retrieved. I'm assuming this is happening because there are two implementations of
A null check on |
For what it's worth, this bubbled back up, only this time it was with |
So, I came back to this topic now that we finally moved our service to .NET Core 3.1 and while the error is different, the scenario still doesn't work. Is that the accepted consensus for 3.1? I was hopeful after @javiercn mentioned that it was fixed in 3.0. |
A bit extra context that I found: the conflict seems to be not just between AzureAD and AzureADB2C, if I add AzureAD and plain OIDC ( |
One final comment from me: I tried the Microsoft.Identity.Web package and it seems to do the trick, no changes to my configuration, minor changes to my services configuration ( |
@alexvy86 : we are very glad to read this (that was one of the goals of Microsoft.Identity.Web). Would you be willing to share (generic) configuration/code snippets to help others to achieve the same? (we intend to document it in MIcrosoft.Identity.Web documentation, but given you have done it, this would be a good head start :)). |
Sure, give me a couple of days and I should be able to clean up my app to work as a generic example. |
Thank you @alexvy86 ! |
Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue. This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue! |
I've been struggling with this scenario for a bit and I'm starting to believe it might not be supported (at least in 2.2?), so I wanted to see if anyone can confirm that or point me on the right direction.
What I want to achieve is to allow different tenants in my multi-tenant application, to use different authentication providers (AzureAD for one, AzureAD B2C for another).
I'm registering separate schemes (Authentication, OpenID, Cookie, etc) for each tenant, like this (and similar for AzureAD):
If I only call
AddAzureAD()
orAddAzureADB2C()
on the authentication builder, things work as expected. But if I call both, I start getting the error below when I make any request to my application (e.g. even just for the favicon). Of note, it doesn't happen immediately at startup, but only when I make the first request:The order in which I register the AzureAD and AzureADB2C providers doesn't seem to matter.
I looked at the code for
AddAzureAD()
andAddAzureADB2C
(forrelease/2.2
) and I started suspecting that the issue comes from the fact that they both try to register singleton configurators forOpenIdConnectOptions
(here and here), so maybe one of them isn't getting that object configured as it expects it to be. Inmaster
, those changed fromTryAddSingleton
toTryAddEnumerable
(here and here). While I don't yet understand how each provider would get the correctOpenIdConnectOptions
in this scenario, based on #4635 I imagine it might help...? @javiercn I hope it's not too intruding to tag you directly, but since you found the problem for #4635 and I think this might be similar I thought it might be useful to include you.For completeness, this is the relevant section of my
appsettings.json
(which I believe is fine, because as I said, either AzureAD or AzureADB2C on its own works correctly).The text was updated successfully, but these errors were encountered: