Repository navigation
Don't short-circuit the in-memory token cache serialization provider - #3970
Merged
Gladwin Johnson VR (gladjohn) merged 4 commits intoJul 24, 2026
Merged
Gladwin Johnson VR (gladjohn) merged 4 commits into
Gladwin Johnson VR (gladjohn) merged 4 commits into
Conversation
AddInMemoryTokenCache(s) previously switched MSAL to its opaque static cache and skipped Initialize() for MsalMemoryTokenCacheProvider, so the IMemoryCache serializer was never used. This disabled the GetSuggestedCacheKey partitioning hook, AbsoluteExpirationRelativeToNow, and the memory-cache SizeLimit, leaving the app-token cache unbounded with no eviction (a driver behind very large single-partition app-token caches). Initialize the provider for all cache types and enable MSAL's shared/static cache only when required (agent CCAs, or the new opt-out). Adds MsalMemoryTokenCacheOptions.UseSharedCache (default false) to restore the legacy static-cache behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2d02b5ca-22b5-4b48-840a-de79786b3f9d
… opt-out Unit tests: MsalMemoryTokenCacheOptions.UseSharedCache defaults to false; MsalMemoryTokenCacheProvider.UseSharedCache reflects the option. Behavior tests: an app token acquired through the default in-memory cache is written to the backing IMemoryCache (serialization provider wired), and with UseSharedCache=true nothing is written (legacy static cache path). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2d02b5ca-22b5-4b48-840a-de79786b3f9d
Bogdan Gavril (bgavrilMS)
approved these changes
Jul 23, 2026
Bogdan Gavril (bgavrilMS)
requested changes
Jul 23, 2026
…ndedCache Address review feedback: rename UseSharedCache -> UseFastUnboundedCache, move it off MsalMemoryTokenCacheProvider/Options onto MicrosoftIdentityOptions (which is config-bindable), and flow it into MergedOptions via UpdateMergedOptionsFromMicrosoftIdentityOptions (mirroring EnableCacheSynchronization). TokenAcquisition now reads mergedOptions.UseFastUnboundedCache. It can be set via configuration under the AzureAd section (UseFastUnboundedCache: true). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2d02b5ca-22b5-4b48-840a-de79786b3f9d
Bogdan Gavril (bgavrilMS)
approved these changes
Jul 24, 2026
Gladwin Johnson VR (gladjohn)
deleted the
gladjohn/inmemory-cache-no-shortcircuit
branch
July 24, 2026 17:07
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.
Summary
When an app registers
AddInMemoryTokenCache/AddInMemoryTokenCaches,TokenAcquisitionbypasses the registeredIMemoryCacheserialization provider and instead switches MSAL to its opaque static (process-wide) cache:Initialize()is what wires MSAL's serialization callbacks, so skipping it for the in-memory provider means:IMemoryCacheis never used (tokens live in MSAL's static dictionary),GetSuggestedCacheKey(the cache-key partitioning hook) is a silent no-op,MsalMemoryTokenCacheOptions.AbsoluteExpirationRelativeToNowand the memory-cacheSizeLimitare silently ignored,This is the IdWeb-side driver behind very large single-partition app-token caches for high-cardinality / token-broker workloads (one
{clientId}_{tenantId}app-token partition observed at 164,663 tokens;AcquireTokenForClientbecomes O(N) over the partition, with saves serializing under MSAL's cache semaphore). Related: #3961.Change
Initialize the token cache provider for all cache types (so the in-memory provider's serialization callbacks — including
GetSuggestedCacheKeyand per-entry expiry — actually run), and enable MSAL's shared/static cache only when it is actually required:Agent CCAs continue to use the shared/static cache (MSAL forbids combining the internal shared cache with external serialization). Now
GetSuggestedCacheKey, entry TTL, and size limits actually work.Opt-in (config-driven)
A new
MicrosoftIdentityOptions.UseFastUnboundedCacheflag restores the previous fast, process-wide static cache. It is wired the same way asEnableCacheSynchronization— a config-bound option that flows intoMergedOptions(UpdateMergedOptionsFromMicrosoftIdentityOptions) and is read inTokenAcquisition. So it is settable from configuration, not just code:false(the new, bounded, serialized behavior).true= MSAL static/shared cache: fast, unbounded (no eviction), ignores expiry/size options and the partitioning hook.Behavior change (minor)
Consumers who set
AbsoluteExpirationRelativeToNow/SizeLimiton the in-memory cache previously had them ignored; they now take effect (tokens may expire/evict where they previously persisted for the process lifetime). Opt out viaUseFastUnboundedCacheif the old semantics are required.Files
src/Microsoft.Identity.Web.TokenAcquisition/TokenAcquisition.cs— un-short-circuit +UseFastUnboundedCachegatesrc/Microsoft.Identity.Web.TokenAcquisition/MicrosoftIdentityOptions.cs— newUseFastUnboundedCache(config-bindable)src/Microsoft.Identity.Web.TokenAcquisition/MergedOptions.cs— flow it intoMergedOptionssrc/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/{NetCore,NetFramework}/PublicAPI.Unshipped.txtTesting
Unit suite green across TFMs (local):
Two behavior tests assert: (1) an app token acquired through the default in-memory cache is written to the backing
IMemoryCache(serialization provider wired), and (2) withUseFastUnboundedCache = truenothing is written (static cache used). An initial naive "always initialize" version failed 9TokenAcquisitionTests.Agent*tests withMsalClientException: "You configured MSAL cache serialization at the same time with internal caching options. These are mutually exclusive.", which drove theusesSharedInternalCachegate. Integration/E2E were not run (require lab credentials).Open item
UseFastUnboundedCacheonMicrosoftIdentityOptionsflows intoMergedOptionsin every host (DefaultTokenAcquisitionHost.GetOptionsresolves theMicrosoftIdentityOptionsmerger), so it works whenever the value is set. Config-binding coverage depends on the entrypoint:TokenAcquirerFactory.GetDefaultInstance("AzureAd")binds the section only toMicrosoftIdentityApplicationOptions. Confirming the MISE-native config entrypoint is a follow-up (if it binds onlyMicrosoftIdentityApplicationOptions, the flag would also need to live inMicrosoft.Identity.Abstractions).🤖 Prototyped during an investigation with GitHub Copilot.