microsoft-identity-web: Problem trying to support two domains for Azure Ad B2C (Protected Web API)
Microsoft.Identity.Web Library
Microsoft.Identity.Web
Microsoft.Identity.Web version
2.13.4
Web app
Sign-in users
Web API
Protected web APIs (validating tokens)
Token cache serialization
In-memory caches
Description
We have an exisiting Protected Web API that supports calls from Azure Ad accounts and Azure B2C.
We want to use Azure Front Door to create a custom name so that in our app the user logs into a domain such as https://account.dev.ourproduct.com rather than https://ourproductdevb2c.b2clogin.com. We will need to support existing clients as well as new ones that are logging in from the custom domain.
I have successfuly converted over our Azure Function away from “EasyAuth” to use System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler and Microsoft.IdentityModel.Protocols.ConfigurationManager. Using a TokenValidationParameters I set multiple ValidIssuers covering the old and new domains, I retrieve the OpenIdConnectConfiguration explicity using a url build with the custom domain and a ConfigurationManager<OpenIdConnectConfiguration>.GetConfigurationAsync call. This all works and supports both domains.
In the examples I have replaced our tenant name with “OurTenantName” and our tenant id with “OurTenantId” obviously this is normally a GUID.
The “Protected Web API” does not work. It is an ASPNET Core Web API. I have included our startup code in “Relevant code snippets”
Reproduction steps
If i set the jwtBearerOptions.Authority to be the cusom domain that will work for clients that signed in and got their tokewn against that custom domain. However it will fail agsinst clients that signed in against the domain “https://OurTenantName.b2clogin.com”
If i switch the Authority the opposite happens, old clients work, new ones do not.
Error message
Whether it works or not I can see one IDX40001 for every call in the VS2022 output window running on my desktop
IDX40001: Issuer: ‘https://Our.CustomDomain/TenantId/v2.0/’, does not match any of the valid issuers provided for this application.
or
IDX40001: Issuer: ‘https://OurTenantName/TenantId/v2.0/’, does not match any of the valid issuers provided for this application.
In Application Insights I get much more information
IDX10264: Reading issuer signing keys from validation parameters and configuration. IDX10265: Reading issuer signing keys from configuration. IDX10243: Reading issuer signing keys from validation parameters. IDX10243: Reading issuer signing keys from validation parameters. IDX10265: Reading issuer signing keys from configuration. IDX10239: Lifetime of the token is valid. IDX40001: Issuer: ‘https://OurTenantName.b2clogin.com/OurTenantId/v2.0/’, does not match any of the valid issuers provided for this application. Bearer was not authenticated. Failure message: IDX40001: Issuer: ‘https://OurTenantName.b2clogin.com/OurTenantId/v2.0/’, does not match any of the valid issuers provided for this application.
Id Web logs
No response
Relevant code snippets
services.AddAuthentication()
.AddMicrosoftIdentityWebApi(
jwtBearerOptions =>
{
jwtBearerOptions.Audience = "https://OurAzureAppName.BreatheRMDevB2C.onmicrosoft.com/user_impersonation";
////jwtBearerOptions.Authority = "https://OurB2cTenantName.b2clogin.com/tfp/OurTendantId/B2C_1A_signin_only";
jwtBearerOptions.Authority = "https://Our.CustomDomain/tfp/OurTendantId/B2C_1A_signin_only";
jwtBearerOptions.TokenValidationParameters.ValidAudiences = new string[2]
{
"OurAppClientId",
"https://OurAzureAppName.OurB2cTenantName.onmicrosoft.com/user_impersonation",
};
},
microsoftIdentityOptions =>
{
microsoftIdentityOptions.ClientId = "OurAppClientId";
microsoftIdentityOptions.Instance = "https://account.dev.breatherm.com";
microsoftIdentityOptions.TenantId = "OurTendantId";
},
"B2CScheme",
true);
### Regression
_No response_
### Expected behavior
The calls would be valid from either domain
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15
@munkii: do I understand that, even the validation passes, you get logs with errors, and you would like to stop that?
If you want to write your own issuer validator, you need to throw new SecurityTokenInvalidIssuerException when the validation fails.
Here is some code showing how the AadIssuerValidator works (the default in IdWeb): https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/dev/src/Microsoft.IdentityModel.Validators/AadIssuerValidator/AadIssuerValidator.cs