You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] 'Scheme already exists: Bearer' when trying to setup both AAD and AAD B2C authย #429
Which version of Microsoft Identity Web are you using?
0.2.3-preview
Where is the issue?
I'm trying to make my ASP.NET Core Web API compatible with both AAD tokens issued on behalf of applications as well as AAD B2C tokens issued on behalf of users, but run into errors when trying to configure both entries in my appsettings.json file. If I only initialize AddMicrosoftWebApi once, then I get issues verifying JWT signature when the token is generated using the identity provider that was left out.
Is this a new or an existing app?
c. This is a new app
You added twice the same authentication scheme. You'd need to use a different name for the jwtBearerScheme parameter in one of your calls. for instance:
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[1]
Failed to validate the token.
Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10501: Signature validation failed. Unable to match key: kid: 'huN95IvPfehq34GzBDZ1GXGirnM'.
One thought that just occurred to me is if it's even possible to support passing different Authorization: Bearer tokens to the same Web Api that are issued by different providers, i.e. one from Active Directory and another from AAD B2C?
EDIT: This documentation section suggests it should be possible, but unclear how to set it up using Microsoft Identity Web.
The IDX10501 was thrown when the issuer signing key was found but didn't match the token. I think all the keys were retrieved from IDP but the wrong one is used to validate the token.
Strangely enough I get a different error when I switch the order of AddMicrosoftWebApi() calls, so if I first setup AzureAdB2C instead of AzureAd, I'll get a different error:
System.InvalidOperationException: IDX20803: Unable to obtain configuration from:
'https://login.microsoftonline.com/mytenant.onmicrosoft.com/B2C_1_SignIn/v2.0/.well-known/openid-configuration'.
It appears that the Instance configuration is overridden with the second call to AddMicrosoftWebApi(), so instead of using "Instance": "https://mytenant.b2clogin.com" it uses "Instance": "https://login.microsoftonline.com/".
Tymek Majewski (@tymtam2) The second stage is separate from the first in the same way Authorization is separate from Authentication in ASP.NET. The problems I'm describing all occur in the first stage when attempting to authenticate Bearer tokens created by two different issuers: AAD and AAD B2C.
You can see in your first code snippet that the docs show how to specify two different authorities. The issue I'm seeing with this library is that .AddMicrosoftWebApi(this.Configuration, "AzureAdB2C") should handle that part, but there's likely a bug in how it pulls and stores the configuration.
You'll need to fill in your own AAD and AAD B2C configuration in appsettings.json. Once you do, you'll notice that when you try to hit the authorized WeatherController endpoint, you'll get an error that suggests it's not properly pulling in the configuration.
EDIT: Specifically, this error I shared above:
System.InvalidOperationException: IDX20803: Unable to obtain configuration from:
'https://login.microsoftonline.com/mytenant.onmicrosoft.com/B2C_1_SignIn/v2.0/.well-known/openid-configuration'.
I wonder if it's related to this section of code within the AddMicrosoftWebApi extension method:
Which seems to apply a configuration to the services. Executing AddMicrosoftWebApi(Configuration, "") twice seems to merge the two sets together, the final config seems to depend on the order that you add them.
Looking at the services collection shows only one MicrosoftIdentityOptions configuration.
When we call AddMicrosoftWebApi twice (for each configuration), Line 81 adds two options delegates to the service collection. Both configure actions are run, and second one overwrites the first. Then Line 94 resolves IOptions<MicrosoftIdentityOptions> as merged options.
Chris Ross (@Tratcher), I tried using the IOptionsSnapshot on Line 94, but get an exception of unable to resolve scoped service from root provider. Is there a better pattern to retrieve named options there?
Thanks everyone for feedback. A fix is in PR #475 (which will be included in the next release). Also added a small section to the wiki related to this.
I'm using the version 1.5.1 and there is still a problem with a custom Bearer scheme for a single AzureAD authentication middleware . Here is a repro project: https://github.com/lnaie/azuread-poc.
For now I'm using a single AzureAd auth middleware in the API project, but the goal is to get to use 2 of them, as many pointed out here, one with the default Bearer scheme (that works) and one with a custom scheme (that I can't get it to work even with a single middleware).
I need help with this.
Activity
jmprieur commentedon Aug 10, 2020
@AzureAD/azure-ad-app-content-authors
You added twice the same authentication scheme. You'd need to use a different name for the
jwtBearerSchemeparameter in one of your calls. for instance:BTW, the default one will be "Bearer" (JwtBearerDefaults.AuthenticationScheme).
pheuter commentedon Aug 10, 2020
Jean-Marc Prieur (@jmprieur) Got it, that makes sense! I am seeing a new error now:
pheuter commentedon Aug 10, 2020
One thought that just occurred to me is if it's even possible to support passing different
Authorization: Bearertokens to the same Web Api that are issued by different providers, i.e. one from Active Directory and another from AAD B2C?EDIT: This documentation section suggests it should be possible, but unclear how to set it up using Microsoft Identity Web.
pmaytak commentedon Aug 11, 2020
The IDX10501 was thrown when the issuer signing key was found but didn't match the token. I think all the keys were retrieved from IDP but the wrong one is used to validate the token.
George Krechar (@GeoK) Maria Furman (@mafurman) BrentSchmaltz (@brentschmaltz)
If necessary, you can use an overload of
AddMicrosoftWebApito configure options likeTokenValidationParameters:pheuter commentedon Aug 11, 2020
Strangely enough I get a different error when I switch the order of
AddMicrosoftWebApi()calls, so if I first setupAzureAdB2Cinstead ofAzureAd, I'll get a different error:It appears that the Instance configuration is overridden with the second call to
AddMicrosoftWebApi(), so instead of using"Instance": "https://mytenant.b2clogin.com"it uses"Instance": "https://login.microsoftonline.com/".appsettings.json:
{ "AzureAd": { "Instance": "https://login.microsoftonline.com/", "Domain": "mytenant.onmicrosoft.com", "ClientId": "<UUID>", "TenantId": "<UUID>" }, "AzureAdB2C": { "Instance": "https://mytenant.b2clogin.com", "Domain": "mytenant.onmicrosoft.com", "ClientId": "<UUID>", "SignUpSignInPolicyId": "B2C_1_SignIn" } }tymtam2 commentedon Aug 13, 2020
The doco Mark Fayngersh (@pheuter) refers to (Use multiple authentication schemes) uses 2 stage process:
Step 1:
Step 2:
When using
AddMicrosoftWebApiAuthenticationorAddMicrosoftWebApi, would the 2nd step be required?pheuter commentedon Aug 13, 2020
Tymek Majewski (@tymtam2) The second stage is separate from the first in the same way Authorization is separate from Authentication in ASP.NET. The problems I'm describing all occur in the first stage when attempting to authenticate Bearer tokens created by two different issuers: AAD and AAD B2C.
You can see in your first code snippet that the docs show how to specify two different authorities. The issue I'm seeing with this library is that
.AddMicrosoftWebApi(this.Configuration, "AzureAdB2C")should handle that part, but there's likely a bug in how it pulls and stores the configuration.jmprieur commentedon Aug 13, 2020
Mark Fayngersh (@pheuter): do you have repro steps so that we can debug? a repro project?
pheuter commentedon Aug 13, 2020
Jean-Marc Prieur (@jmprieur) I just created this repo: https://github.com/pheuter/IdentityWebRepro
You'll need to fill in your own AAD and AAD B2C configuration in appsettings.json. Once you do, you'll notice that when you try to hit the authorized
WeatherControllerendpoint, you'll get an error that suggests it's not properly pulling in the configuration.EDIT: Specifically, this error I shared above:
timClyburn commentedon Aug 16, 2020
I wonder if it's related to this section of code within the
AddMicrosoftWebApiextension method:Which seems to apply a configuration to the services. Executing
AddMicrosoftWebApi(Configuration, "")twice seems to merge the two sets together, the final config seems to depend on the order that you add them.Looking at the services collection shows only one MicrosoftIdentityOptions configuration.
pmaytak commentedon Aug 18, 2020
Thanks everyone for your investigations. Tim Clyburn (@timClyburn), you're right.
When we call
AddMicrosoftWebApitwice (for each configuration), Line 81 adds two options delegates to the service collection. Both configure actions are run, and second one overwrites the first. Then Line 94 resolvesIOptions<MicrosoftIdentityOptions>as merged options.Chris Ross (@Tratcher), I tried using the
IOptionsSnapshoton Line 94, but get an exception of unable to resolve scoped service from root provider. Is there a better pattern to retrieve named options there?microsoft-identity-web/src/Microsoft.Identity.Web/WebApiExtensions/WebApiAuthenticationBuilderExtensions.cs
Lines 58 to 95 in 876cb86
14 remaining items
pmaytak commentedon Aug 28, 2020
Thanks everyone for feedback. A fix is in PR #475 (which will be included in the next release). Also added a small section to the wiki related to this.
jmprieur commentedon Aug 28, 2020
Thanks everybody
Peter (@pmaytak) it might be worth describing the appconfig.json, and possibly the attribute
Thx
jennyf19 commentedon Sep 11, 2020
Included in 0.4.0-preview release
lnaie commentedon Feb 11, 2021
I'm using the version 1.5.1 and there is still a problem with a custom Bearer scheme for a single AzureAD authentication middleware . Here is a repro project: https://github.com/lnaie/azuread-poc.
For now I'm using a single AzureAd auth middleware in the API project, but the goal is to get to use 2 of them, as many pointed out here, one with the default Bearer scheme (that works) and one with a custom scheme (that I can't get it to work even with a single middleware).
I need help with this.
jmprieur commentedon Feb 11, 2021
Lucian Naie (@lnaie) : yes; this is a duplicate of #955, which we'll address (there is a PR. you might want to try the branch)
jennyf19 commentedon May 24, 2021
Included in 1.11.0 release and documentation here.