Allow multiple Instances for AzureB2C

Viewed 23

We need to change the Instance-URL (so the user sees another URL) for our AzureB2C. Because the client is an App that still could have the old configuration we do not simply want to replace the instance configuration but allow both for a while.

This is our Approach. Config:

"AzureAdB2COld:ClientId": "GUID0",
"AzureAdB2COld:Domain":"ourdomain.onmicrosoft.com",
"AzureAdB2COld:Instance":"https://login.olddomain.com",
"AzureAdB2COld:SignUpSignInPolicyId":"B2C_1A_signin"
"AzureAdB2COld:TokenValidationParameters:ValidAudiences:0":"GUID1" ,
"AzureAdB2COld:TokenValidationParameters:ValidAudiences:1":"GUID2" 
    
"AzureAdB2CNew:ClientId": "GUID0",
"AzureAdB2CNew:Domain":"ourdomain.onmicrosoft.com",
"AzureAdB2CNew:Instance":"https://login.newdomain.com",
"AzureAdB2CNew:SignUpSignInPolicyId":"B2C_1A_signin"
"AzureAdB2CNew:TokenValidationParameters:ValidAudiences:0":"GUID1" ,
"AzureAdB2CNew:TokenValidationParameters:ValidAudiences:1":"GUID2" 

(as you can see, only the "Instance"-Parameter is different) We Configure this in Startup by adding two Authentications to the services:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
    Configuration.Bind("AzureAdB2COld", options);
    options.TokenValidationParameters.NameClaimType = "email";
    options.Events = new JwtBearerEvents()
    {
        OnTokenValidated = async (context) =>
        {
            var principalService = context.HttpContext.RequestServices.GetRequiredService<PrincipalService>();
            await principalService.InitAsync(context.Principal);
        }
    };
},
options => { Configuration.Bind("AzureAdB2COld", options); }
);

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
    Configuration.Bind("AzureAdB2CNew", options);
    options.TokenValidationParameters.NameClaimType = "email";
    options.Events = new JwtBearerEvents()
    {
        OnTokenValidated = async (context) =>
        {
            var principalService = context.HttpContext.RequestServices.GetRequiredService<PrincipalService>();
            await principalService.InitAsync(context.Principal);
        }
    };
},
options => { Configuration.Bind("AzureAdB2CNew", options); }
);

While each of the configurations alone work fine, we get an error if we try to add both:

InvalidOperationException: Scheme already exists: Bearer

Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(string name, Action configureBuilder)

Is there a way to allow both Instances through configuration at the same time?

0 Answers
Related