Skip to content

Add CacheKey to AppTokenProviderResult for cache partitioning#6019

Draft
trwalke wants to merge 1 commit into
mainfrom
agents/update-app-token-provider-cachekey
Draft

Add CacheKey to AppTokenProviderResult for cache partitioning#6019
trwalke wants to merge 1 commit into
mainfrom
agents/update-app-token-provider-cachekey

Conversation

@trwalke
Copy link
Copy Markdown
Member

@trwalke trwalke commented May 20, 2026

This pull request introduces support for partitioning the access token cache in MSAL by allowing an AppTokenProvider to specify a custom cache key via the new CacheKey property in AppTokenProviderResult. This enables scenarios where tokens should be cached separately based on provider-specific criteria. The changes also include updates to the public API, internal logic for cache key handling, and comprehensive unit tests to verify the new behavior.

App Token Provider Cache Partitioning:

  • Added a new CacheKey property to AppTokenProviderResult, allowing the app token provider to partition the access token cache. This value is included in the cache key components under "appTokenProviderKey", ensuring tokens with different cache keys are stored separately.
  • Updated the public API surface to include the new CacheKey property in all relevant PublicAPI.Unshipped.txt files.
  • Introduced the constant AppTokenProviderCacheKey in Constants.cs to standardize the cache key component name.
  • Modified the token acquisition flow in ClientCredentialRequest to add the CacheKey to AuthenticationRequestParameters.CacheKeyComponents if provided by the app token provider.
  • Changed the setter for CacheKeyComponents in AuthenticationRequestParameters to internal to allow internal updates.

Testing:

  • Added unit tests to verify that tokens are cached in separate partitions when different cache keys are provided, and that no extra cache key is added when CacheKey is not set.Fixes #

Changes proposed in this request

Testing

Performance impact

Documentation

  • All relevant documentation is updated.

@trwalke trwalke requested a review from a team as a code owner May 20, 2026 09:08
Copilot AI review requested due to automatic review settings May 20, 2026 09:08
@trwalke trwalke marked this pull request as draft May 20, 2026 09:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for access token cache partitioning for AppTokenProvider by introducing a provider-supplied CacheKey that becomes part of MSAL’s internal access token cache key components.

Changes:

  • Added AppTokenProviderResult.CacheKey to allow partitioning of app token provider access token cache entries.
  • Updated client credential flow to include the provider cache key in AuthenticationRequestParameters.CacheKeyComponents.
  • Added unit tests validating partitioning behavior and ensuring no cache key component is added when CacheKey is absent.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/Microsoft.Identity.Test.Unit/AppTokenProviderTests.cs Adds tests verifying cache partitioning via provider CacheKey and default behavior when unset.
src/client/Microsoft.Identity.Client/PublicApi/*/PublicAPI.Unshipped.txt Updates public API baselines to include AppTokenProviderResult.CacheKey.
src/client/Microsoft.Identity.Client/Internal/Requests/ClientCredentialRequest.cs Injects provider CacheKey into request cache key components.
src/client/Microsoft.Identity.Client/Internal/Requests/AuthenticationRequestParameters.cs Allows internal setting of CacheKeyComponents so internal flows can update it.
src/client/Microsoft.Identity.Client/Internal/Constants.cs Introduces a constant for the reserved cache key component name.
src/client/Microsoft.Identity.Client/Extensibility/AppTokenProviderResult.cs Adds the new public CacheKey property with XML docs.

Comment on lines +216 to +217
t.AdditionalCacheKeyComponents.TryGetValue("appTokenProviderKey", out string _),
"Cached AT does not contain the 'appTokenProviderKey' cache component.");
}

var keysSeen = allTokens
.Select(t => t.AdditionalCacheKeyComponents["appTokenProviderKey"])
Comment on lines +256 to +258
token.AdditionalCacheKeyComponents.ContainsKey("appTokenProviderKey");
Assert.IsFalse(hasProviderKey,
"When AppTokenProviderResult.CacheKey is not set, the 'appTokenProviderKey' component must not be added.");
Comment on lines +34 to +36
/// to partition the cache. When provided, this value is stored in the request's
/// <c>CacheKeyComponents</c> under the key <c>"appTokenProviderKey"</c> and contributes to the
/// computed access token cache key.
{
if (AuthenticationRequestParameters.CacheKeyComponents == null)
{
AuthenticationRequestParameters.CacheKeyComponents = new SortedList<string, string>();
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.

2 participants