question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Easier token caching

See original GitHub issue

Is your feature request related to a problem? Please describe. Token caching is hard, because it involves writing a lot of boiler plate code and because of bad initial design, where a notification mechanism was used instead of an object / interface.

Microsoft.Identity.Web simplifies things this by introducing MsalAbstractTokenCacheProvider which deals with the complex Before / After notifications and focuses on Read / Write operations:

WriteCacheBytesAsync(string cacheKey, byte[] bytes);
Task<byte[]> ReadCacheBytesAsync(string cacheKey);

Solution 1

Introduce MsalAbstractTokenCacheProvider in MSAL.

IConfidentialClientApplication cca = ConfidentialClientApplicationBuilder
    .Create("id")
    .WithClientSecret("secret")
    .Build();

// L1TokenCache can live in MSAL
MsalAbstractTokenCacheProvider adapter = new L1TokenCache(MaxSize = "2Gb");
adapter.Init(cca.AppTokenCache);

// OR RedisTokenCacheAdapter lives in Microsoft.Identity.Web
MsalAbstractTokenCacheProvider adapter = new RedisTokenCacheAdapter();
adapter.Init(cca.AppTokenCache);

In addition MSAL can provide several implementations out of the box:

  • a partitioned in memory cache for CCA
  • an L1 cache for CCA with eviction options (this already exists in M.I.W based on MemoryCache, but an Msal implementation is possible if using Wilson’s cache with event based evictions)
  • MSAL ex cache for public clients (which could move entirely in MSAL by the way)

M.I.W. can also rely on this infrastructure to make it easier to consume their token caches.

Solution 2


interface ITokenCacheSerialization
{ 
    void Write(string cacheKey, byte[] payload, CacheHints hints));
    byte[] Read(string cacheKey);
    void Delete(string cacheKey);
}

// then MSAL would use it as 
 IConfidentialClientApplication cca = ConfidentialClientApplicationBuilder
    .Create(s_clientIdForConfidentialApp) // <-- can even be seen as a mandatory param for apps
    .WithTokenCacheSerialization(ITokenCacheSerialization)
    .WithClientSecret(s_confidentialClientSecret)
    .Build();

In MSAL 4, we keep the 2 ways of serialization. In MSAL 5, we rely exclusively on the new way - deprecate BeforeAccess / AfterAccess etc. and provide an adapter:


// MSAL 5 migration help
ITokenCacheSerialization seri = new CallbackAdapter() { BeforeAccess = ((notificatation) => { /* old logic */} AfterAccess= ((notificatation) => { /* old logic */} };

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jmprieurcommented, Jun 30, 2021
0reactions
bgavrilMScommented, Jun 14, 2021

Sorry, haven’t see this. Let’s continue the discussion on the relevant thread.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Token cache serialization (MSAL.NET) - Microsoft Entra
Creates a temporary cache in memory for token storage and retrieval. In-memory token caches are faster than other cache types, but their tokens...
Read more >
Acquire and cache tokens with Microsoft Authentication ...
There are several ways to acquire a token by using the Microsoft Authentication Library (MSAL). Some require user interaction through a web ...
Read more >
Cache Authorized User Access Token Session
When the User Access feature is enabled the password is prompted when trying to access any of the protected sections it is applied...
Read more >
Api.cache in M2M flow action question
A simple example would be caching tokens for an external API: Without caching, you would have to request a new token during every...
Read more >
Add rate limiting and cache for m2m token authentication ...
Feature: Provide a short title of your feature request/feedback. Allow cache and rate limiting by client id for m2m token authentication ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found