I understand it is possible using the custom JWT Providers to add multiple Authentication Schemes in one application. In the following documentation: https://github.com/AzureAD/microsoft-identity-web/wiki/Multiple-Authentication-Schemes, it is stated that "Microsoft Identity Web now supports multiple authentication schemes, as of v.1.11.0."
My problem is as follows: I would like to use the Bearer token as a method of authentication from an Azure AD Resource in one tenant, and a Azure AD B2C resource in another tenant.
I have tried the following:
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
configuration.Bind("AzureAdB2C", options);
}, options =>
{
configuration.Bind("AzureAdB2C", options);
}, Constants.Bearer, true);
builder.Services.AddAuthentication()
.AddMicrosoftIdentityWebApi(options =>
{
configuration.Bind("AzureAd", options);
options.TokenValidationParameters.ValidAudiences = ["some-valid-audience-value-i-am-hiding-from-stack-overflow"];
}, options => { configuration.Bind("AzureAd", options); }, Constants.Bearer, true );
This throws the following exception:
System.InvalidOperationException: 'Scheme already exists: Bearer'
Following this, I can change the name of the Constants.Bearer to a secondary value to avoid this exception: IE:" "Bearer2". Now, only the first AzureAdB2C tokens work, and the secondary azure ad tokens are failing at this point.
Is there a way to make AddMicrosoftIdentityWebApi attempt to decode two separate bearer tokens from two separate resources?
EnableTokenAcquisitionToCallDownstreamApi.