Fix AadIssuerValidator's handling of trailing forward slashes #2361
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes errors like the following reported by multiple customers at dotnet/aspnetcore#51005 when they tried to upgrade their app using
AddMicrosoftIdentityWebApp
to .NET 8.While
https://login.microsoftonline.com/{TenantId}/v2.0/.well-known/openid-configuration
, responds with"issuer": "https://login.microsoftonline.com/{TenantId}/v2.0"
and issues JWTs withiss
claims containing no trailing slashes, if you use a custom B2C authority likehttps://{TenantName}.b2clogin.com/{TenantName}.onmicrosoft.com/B2C_1_SignUpSignIn/v2.0/.well-known/openid-configuration
you will get"issuer": "https://TenantName}.b2clogin.com/{TenantId}/v2.0/"
responses andiss
claims with trailing slashes.This is causing upgrades of Azure B2C customers to .NET 8 to fail because
AadIssuerValidator.GetEffectiveConfigurationManager()
does not account for trailing slashes. This causes it to query V1 metadata for a V2 token, which causes a mismatch because the V1 metadata does not include a trailing forward slash for the issuer while the V2 token does include a trailing forward slash.While it looks like this bug has been around for a while, I think the reason customers haven't seen this until upgrading to .NET 8 is because the
OpenIdConnectHandler
has stopped settingTokenValidationParameters.ValidIssuers
. It is now expected expected settingTokenValidationParameters.ConfigurationMager
is sufficient as of dotnet/aspnetcore#49542.In the particular case of people using
AadIssuerValidator
, this PR is sufficient to fix regression becauseAadIssuerValidator.Validate
falls back to querying theConfigurationManager
itself rather than completely rely onOpenIdConnectHandler.HandleRemoteAuthenticateAsync()
having done that already.However, I am concerned that there could be other custom
TokenHander
orTokenValidationParameters.IssuerValidator
callbacks that read fromTokenValidationParameters.ValidIssuers
other thanAadIssuerValidator
, and that don't have the logic to query theConfigurationManager
itself since it was never before necessary.I'm wondering if we shouldn't also just update the
else if (_configuration != null)
to beif (_configuration != null)
in OpenIdConnectHandler.ValidateTokenUsingHandlerAsync, JwtBearerHandler.SetupTokenValidationParametersAsync and WsFederationHandler.SetupTokenValidationParametersAsync to be extra safe. I assume this would have perf implications though.@jennyf19 @keegan-caruso @brentschmaltz @eerhardt @mkArtak