-
Notifications
You must be signed in to change notification settings - Fork 219
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
[Bug] Protecting WebAPI does not work if one wants to use multiple bearer schemes, e.g. for AAD *and* B2C #468
Comments
@astaykov Do you try? services.AddAuthentication()
.AddMicrosoftWebApi(Configuration, "AzureAdB2C", "B2C")
.AddMicrosoftWebApi(Configuration, "AzureAd", "v2.0"); You make a good point about the options. We might need to try to use named options (with an IOptionsSnapshot) for MicrosoftIdentityOptions to support this scenario @jennyf19 and @pmaytak @astaykov; Does your work around fully work for you? is it ok to prioritize this fix after our 0.3.0-preview? |
Thanks @astaykov for investigation and a write-up. This is also a duplicate of #429. I wrote a reply there. The issue is with calling
|
Using the suggested approach has absolutely same (erronous) results like in my original approach. |
Included in 0.4.0-preview release |
1 similar comment
Included in 0.4.0-preview release |
Using Microsoft Identity Web 0.2.3-preview protecting WebAPI with multiple Bearer schemes for different authorities.
Referring to this sample for multi bearer auth for Web Apis, it is impossible to use Microsoft.Identity.Web package to accomplish the required setup.
Steps to reproduce:
Create the following configuration file (only relevant sections are listed here for clarity):
Then try to register both AzureAd and AzureAdB2C the following way:
If the order is like here displayed - first registering AzureAdB2C and then registering AzureAd, you will get the following error when calling the API with valid token:
If changing the order - first register the AzureAd and then AzureAdB2C, e.g.:
You will get success (200) calling the WebAPI with Azure AD B2C token, but 401 calling the API using v2.0 Token.
If you only use one of them (either AzureAd only or AzureAdB2C only) - it works.
Also, if you use the Microsoft.Identity.Web extensions to register only one of the providers (AzureAd OR AzureAdB2C), but use the Microsoft.AspNetCore.Authentication.XXX for the other provider, e.g.:
Then the API accepts both AzureAd (v2.0) AND AzureAdB2C tokens.
After some very short search through the code, it seems this registration in WebApiAuthenticationBuilderExtensions might be causing the issue. Because of
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<MicrosoftIdentityOptions>, MicrosoftIdentityOptionsValidation>())
, which is trying to add a singleton ofMicrosoftIdentityOptions
AddMicrosoftWebApiAuthentication
will override any configuration settings that are different from the first registration. If I read correctly the documentation about TryAddEnumerable, it explains the behavior I am observing. But this is just a guess.Expected behavior is that I am able to use
AddMicrosoftWebApiAuthentication
multiple times with different configuration settings. There are a lot of valid scenarios where one would like to have this:client credentials
flow (which is exactly what this sample is about)The text was updated successfully, but these errors were encountered: