Ensuring ValidateCachedTokenAsync works for other apis#5764
Merged
Conversation
bgavrilMS
reviewed
Feb 23, 2026
bgavrilMS
approved these changes
Feb 23, 2026
gladjohn
approved these changes
Feb 24, 2026
tests/Microsoft.Identity.Test.Unit/PublicApiTests/AuthenticationOperationTests.cs
Show resolved
Hide resolved
gladjohn
reviewed
Feb 24, 2026
gladjohn
approved these changes
Feb 24, 2026
This was referenced Mar 9, 2026
This was referenced Mar 16, 2026
Closed
This was referenced Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 pull request introduces a unified approach for validating cached access tokens before use, improving consistency and reliability across authentication flows. The main change is the addition of a new helper method,
ValidateCachedAccessTokenAsync, which centralizes the logic for validating cached tokens using the authentication operation (when supported). This method is now used in multiple request types, and comprehensive unit tests have been added to ensure correct behavior.Token validation improvements:
ValidateCachedAccessTokenAsyncmethod inRequestBase.csto centralize and streamline validation of cached access tokens usingIAuthenticationOperation2. The method returns the original cache item if validation passes, or null if validation fails.ClientCredentialRequest,OnBehalfOfRequest, andCacheSilentStrategyto use the new helper method for validating cached access tokens, replacing previous inline validation logic. [1] [2] [3]Testing enhancements:
AuthenticationOperationTests.csto verify that cached tokens are properly validated and either used or ignored based on the result ofValidateCachedTokenAsync. Tests cover both On-Behalf-Of and silent authentication flows, for both validation success and failure scenarios.Code cleanup:
using Microsoft.Identity.Client.AuthScheme;directive fromClientCredentialRequest.csas part of refactoring.Ensuring ValidateCachedTokenAsync works for other apis