Skip to content

feat(msal): persist the token cache on WebAssembly through IKeyValueStorage - #3162

Merged
kazo0 merged 1 commit into
mainfrom
msal-3-wasm-token-cache
Aug 25, 2026
Merged

kazo0 merged 1 commit into
mainfrom
msal-3-wasm-token-cache

Conversation

@kazo0

@kazo0 kazo0 commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): closes #3158 (layer 3/4 of #3155; relates to #2438)

Stack 3/4 — base: msal-2-runtime-dispatch. Review only this layer's diff. Depends on layer 2 for the provider's runtime dispatch (which decides where the browser branch runs) and on #3153 for IKeyValueStorage on WebAssembly (localStorage / sessionStorage backends). Deliberately deferred upward: the Uno version floor (layer 4).

PR Type

  • Feature

What is the current behavior?

On WebAssembly the MSAL token cache is memory-only: MsalCacheHelper has no browser backend, so the provider skips cache setup and every page reload — or a navigation away and back — means signing in again. Nothing removes cache material on sign-out because there is nothing persisted to remove.

What is the new behavior?

The browser branch serializes the MSAL cache through IKeyValueStorage. On WebAssembly the provider registers SetBeforeAccessAsync / SetAfterAccessAsync callbacks that read and write the serialized cache (MsalTokenCacheStore, key MsalCache_{ClientId}) into the host's default key-value store. Which store is the storage layer's decision — KeyValueStorageConfiguration:BrowserCacheLocation, localStorage by default — so a page reload keeps the session, sessionStorage scopes it to the tab, and MemoryStorage yields the previous in-memory behaviour for free. MsalKeyValueStorage wraps the resolved default store for injection, because a bare IKeyValueStorage registration resolves to the in-memory one.

Sign-out and ITokenCache.Cleared both delete the serialized cache — in a finally, with CancellationToken.None — so a cancelled or failed sign-out cannot leave refresh-token material in the browser.

A warning names the store whenever the cache is persisted unprotected. The serialized cache holds the refresh token; only an Entra spa platform registration caps that token at 24 non-sliding hours, and the how-to now says so as a prerequisite.

Tests. Given_MsalTokenCacheStore (unit): round trip, key layout, corrupt-blob handling, no-op writes. Given_MsalAuthentication (UI, real MSAL against StubEntra): sign-out purges the store, cancelled sign-out purges the store, Cleared purges the store, and a guard that the cache is persisted only on WebAssembly. RuntimeTestsFilter already covers the namespace, so the WebAssembly lane from layer 1 runs the new cases. Spec specs/011 carries the design trail.

Behaviour changes (documented in UpdatingExtensions.md)

WebAssembly sign-in state now survives a reload by default; apps that want the old memory-only behaviour set BrowserCacheLocation to MemoryStorage.

PR Checklist

  • Tested code with current supported SDKs
  • Docs have been added/updated which fit documentation template — browser cache section of the how-to, migration note
  • Unit Tests and/or UI Tests for the changes have been added
  • Wasm UI Tests are not showing any unexpected differences
  • Contains NO breaking changes — see the behaviour change above
  • Updated the Release Notes
  • Associated with an issue (GitHub or internal)

Other information

Verified locally: MSAL.Tests 39/39; package surface builds warning-free on all seven TFMs; desktop runtime suite 17 MSAL + 8 storage = 25/25. The persisted-cache path itself only executes on the WebAssembly lane (desktop asserts it stays off).

🤖 Generated with Claude Code

https://claude.ai/code/session_01EqfQh5ovrPEvFGcERLiLhH

@kazo0
kazo0 force-pushed the msal-3-wasm-token-cache branch from ac7b819 to f080636 Compare August 25, 2026 01:17
@kazo0
kazo0 force-pushed the msal-3-wasm-token-cache branch 2 times, most recently from e00dae1 to a84a1f4 Compare August 25, 2026 01:32
@kazo0
kazo0 force-pushed the msal-3-wasm-token-cache branch from a84a1f4 to 2edb8c2 Compare August 25, 2026 15:37
@kazo0
kazo0 force-pushed the msal-3-wasm-token-cache branch from 2edb8c2 to 4235ee3 Compare August 25, 2026 15:38
@kazo0
kazo0 force-pushed the msal-3-wasm-token-cache branch 2 times, most recently from 58aa847 to 8543148 Compare August 25, 2026 16:44
@kazo0

kazo0 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

Base automatically changed from msal-2-runtime-dispatch to main August 25, 2026 17:24
…torage

Layer 3/4 of the MSAL authentication work (#3139). Before this the MSAL cache
was memory-only in the browser, so every page reload meant signing in again.

- MsalCacheHelper has no browser backend, so on WebAssembly the provider
  registers SetBeforeAccessAsync/SetAfterAccessAsync callbacks that
  serialize the cache through MsalTokenCacheStore into the host's default
  IKeyValueStorage, keyed MsalCache_{ClientId}. Which store that is - and so
  whether the cache survives a reload or a tab close - is the storage layer's
  KeyValueStorageConfiguration:BrowserCacheLocation (localStorage by default);
  this side only reads and writes the blob, so MemoryStorage yields the old
  in-memory behavior for free
- MsalKeyValueStorage wraps the resolved default store for injection, since a
  bare IKeyValueStorage registration resolves to the in-memory one
- Sign-out and ITokenCache.Cleared both delete the serialized cache, in a
  finally and with CancellationToken.None, so a cancelled or failed sign-out
  cannot leave refresh-token material behind
- A warning names the store whenever the cache is persisted unprotected: the
  serialized cache holds the refresh token, and only an Entra `spa`
  registration caps that token at 24 non-sliding hours

Tests: Given_MsalTokenCacheStore (unit) covers the round trip, key layout,
corrupt-blob handling and no-op writes; the UI suite gains the sign-out /
cancelled sign-out / Cleared purge cases and the "persisted only on
WebAssembly" guard, and the WebAssembly lane runs it. Docs: the browser cache
section of the how-to (store choice, the `spa` prerequisite and why it
matters) and the migration note. Spec 011 carries the design trail.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EqfQh5ovrPEvFGcERLiLhH
@kazo0
kazo0 force-pushed the msal-3-wasm-token-cache branch from 8543148 to 2a25f1c Compare August 25, 2026 17:24
@mergify

mergify Bot commented Aug 25, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@kazo0
kazo0 merged commit 8548c46 into main Aug 25, 2026
27 of 28 checks passed
@kazo0
kazo0 deleted the msal-3-wasm-token-cache branch August 25, 2026 18:03
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.

MSAL: persist the token cache on WebAssembly through IKeyValueStorage

3 participants