I have an app writen in .NET CORE 6.0 What I'm trying to achive is that based on what URL is loaded to load specific AzureAdB2C settings. I don't want to have them in appsettings.json
For ex. multiple subdomains use this app, aaaa.test.com | bbbb.test.com | etc
When someone access aaaa.test.com I want to be able to load the specific AzureAdB2C settings, othes settings for bbbb.test.com and so on.
I was able to find solutions for multiple AzureAdB2C settings added in appsettings.json but I need a more dynamic way to load them (from SQL for ex).
I've spent a lot of time trying to find a solution but with no success...
Hard to say, I've have tryied a lots of approches with no success...
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "TokenLogin";
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie("TokenLogin", options =>
{
options.ExpireTimeSpan = DateTime.Now.Subtract(DateTime.UtcNow).Add(TimeSpan.FromDays(2));
options.Cookie.MaxAge = options.ExpireTimeSpan;
options.SlidingExpiration = true;
})
.AddMicrosoftIdentityWebApp(options =>
{
builder.Configuration.Bind("AzureADB2C", options);
//-------> things that I've tried
//var xxx = builder.Services.BuildServiceProvider().GetService<IHttpContextAccessor>().HttpContext;
//var authOptions = xxx.RequestServices.GetRequiredService<IOptionsMonitor<AzureADB2C>>();
////var authOptions = xxx.RequestServices.GetRequiredService<AzureADB2C>();
//options.Instance = authOptions.CurrentValue.Instance;
//options.ClientId = authOptions.CurrentValue.ClientId;
//options.CallbackPath = authOptions.CurrentValue.CallbackPath;
//options.Domain = authOptions.CurrentValue.Domain;
//options.SignUpSignInPolicyId = authOptions.CurrentValue.SignUpSignInPolicyId;
//options.ResetPasswordPolicyId = authOptions.CurrentValue.ResetPasswordPolicyId;
//options.EditProfilePolicyId = authOptions.CurrentValue.EditProfilePolicyId;
//options.SignInScheme = OpenIdConnectDefaults.AuthenticationScheme;
//options.Events ??= new OpenIdConnectEvents();
//options.Events.OnRedirectToIdentityProvider += OnRedirectToIdentityProviderFunc;
//options.Events.OnRedirectToIdentityProvider = context =>
//{
// Your code here
//return Task.CompletedTask;
//};
//options.Instance = authOptions.Instance;
//options.ClientId = authOptions.ClientId;
//options.CallbackPath = authOptions.CallbackPath;
//options.Domain = authOptions.Domain;
//options.SignUpSignInPolicyId = authOptions.SignUpSignInPolicyId;
//options.ResetPasswordPolicyId = authOptions.ResetPasswordPolicyId;
//options.EditProfilePolicyId = authOptions.EditProfilePolicyId;
//authOptions.OnChange(newOptions => {
// if (!settingsWereLoaded)
// {
// //options.Instance = authOptions.CurrentValue.Instance;
// //options.ClientId = authOptions.CurrentValue.ClientId;
// //options.Domain = authOptions.CurrentValue.Domain;
// options.Instance = "https://xxxx.b2clogin.com/tfp/";
// options.ClientId = "XXXXX-XXXX-XXXX-XXXX-XXXX";
// options.Domain = "XXXX.onmicrosoft.com";
// settingsWereLoaded = true;
// }
//});
});
After a lot of research I wasn't able to find a solution, still happy for someone to provide one. Main thing that I wanted to achieve was to be able to update the AzureADB2C settings at the runtime and with this based on a criteria (url / a dynamic prop etc)to be able to use different settings.
Using this approach you'll need to test all flows from AzureAdB2C (as Forgot password / new user etc) and make sure all of these are covered.
I have used another approach, I've removed the:
and use Cookies instead:
I have created a Midelware: SignInIdDcMiddleware.cs and register it. The redirect URI needs to be whitelisted in Azure.
The code for SignInIdDcMiddleware.cs is below, and with the info from here, you can pass the info to a controller that will deal with the authentication using the cookies.
The code for the controller AuthController.cs below, please deal with secure the information, this is just a POC.
And in a *.razor file you can build the AzureADB2C sign in url: