Skip to content

Ensuring ValidateCachedTokenAsync works for other apis#5764

Merged
trwalke merged 4 commits intomainfrom
trwalke/UpdateCacheValidationMethod
Feb 24, 2026
Merged

Ensuring ValidateCachedTokenAsync works for other apis#5764
trwalke merged 4 commits intomainfrom
trwalke/UpdateCacheValidationMethod

Conversation

@trwalke
Copy link
Copy Markdown
Member

@trwalke trwalke commented Feb 19, 2026

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:

  • Added the ValidateCachedAccessTokenAsync method in RequestBase.cs to centralize and streamline validation of cached access tokens using IAuthenticationOperation2. The method returns the original cache item if validation passes, or null if validation fails.
  • Updated ClientCredentialRequest, OnBehalfOfRequest, and CacheSilentStrategy to use the new helper method for validating cached access tokens, replacing previous inline validation logic. [1] [2] [3]

Testing enhancements:

  • Added unit tests in AuthenticationOperationTests.cs to verify that cached tokens are properly validated and either used or ignored based on the result of ValidateCachedTokenAsync. Tests cover both On-Behalf-Of and silent authentication flows, for both validation success and failure scenarios.

Code cleanup:

  • Removed unnecessary using Microsoft.Identity.Client.AuthScheme; directive from ClientCredentialRequest.cs as part of refactoring.Ensuring ValidateCachedTokenAsync works for other apis

@trwalke trwalke enabled auto-merge (squash) February 24, 2026 23:12
@trwalke trwalke merged commit c454ab1 into main Feb 24, 2026
12 checks passed
@trwalke trwalke deleted the trwalke/UpdateCacheValidationMethod branch February 24, 2026 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ensure ValidateCachedTokenAsync Supports Additional APIs

3 participants