-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request] By default confidential client applications should have a cache that behaves better and encourages using serialization adapters #2443
Comments
Some of the work has been done. Here's what I think is left:
ccaBuilder.WithTokenCaching(cacheProvider) Thoughts @jmprieur ? |
|
@bgavrilMS |
@bgavrilMS In what scenarios would a static cache be needed that reusing a CCA instance doesn't cover? |
Closing as a duplicate it's not processed by: |
Is your feature request related to a problem? Please describe.
In Confidential client applications, our guidance is for developers to partition the token caches using the suggested cache key (we tend to say one cache per user, at least for the user token cache).
However, the default cache provided my MSAL.NET is just a flat list of accounts therefore:
Microsoft.Identity.Web provides a good solution, but it's not discoverable when customers are just using MSAL.NET
Describe the solution you'd like
In the description below, we name "incoming token", a token that was passed-in to call a web API, and validated by the web API.
By default, when instantiating a confidential client application, the cache should behave well. For instance, we could plug-in a default token cache serializer that would be based on a Dictionary<string, byte[]>, which would be a dictionary of caches per cache key. This works well in Microsoft.Identity.Web already (See MsalAbstractTokenCacheProvider and MsalMemoryTokenCacheProvider) , but:
MemoryCache
andIDistributedCache
, so we would not bring them to the MSAL.ENT Code anyway.This solution would solve the security issue and partially the performance issue (as the cache key would be right). It would not solve the fact that the cache can be big in memory
Log errors (but do not throw) in the log if the dictionary has more than 200 keys. This error should advise customers to use a token cache serialization, and provide an actionable link:
Log errors (but do not throw) in the log if the dictionary the time it takes to get a token from the cache takes more than x ms. This error should advise customers to use a token cache serialization, and provide an actionable link:
The default token cache provided by MSAL is not designed to be performant. Please use token cache serialization. See https://aka.ms/msal-net-cca-token-cache-serialization.
Describe alternatives you've considered
Additional context
When doing the right portioning of the cache by suggested cache key, the following will happen:
GetAccountsAsync()
will now return an empty collection, as there won't be any longer a flat list of all the accounts. This method should be deprecated. We propose to have an obsolete attribute with an aka.ms linkGetAccountsAsync() should not be used in confidential client application. Use GetAccountAsync with the identifier of the current user instead. For details, see https://aka.ms/msal-net-cca-token-cache-serialization.
This will de facto account for [Enhancement] GetAccountsAsync should no longer be exposed in confidential client applications (via deprecation) #1967 which should be processed at the same time as this issue
In web APIs,
AcquireTokenSilent
will always throw an MsalUiRequiredException, because it will attempt to get a token for a user, whereas the OBO key is the hash of the token. We'd probably want to indicate to the developer that they should not use AcquireTokenSilent in a web API? maybe by logging an error (same aka.ms link as for the other case). This is an optimizaztion anyway as if customers use the proposed pattern they would end up calling OnBehalfOf, which does the cache lookup with the right key (the hash of the incoming token on the web API). This would account for [Enhancement] AcquireTokenSilent should not return token acquired by OBO #1966 that should be processed at the same time as this issueSee also #2447, which implies that the cache also needs to maintain, for OBO a map of user identifier, and the list of corresponding incoming token hashes.
Work done
WithCacheSynchronization(bool)
that defaults totrue
on CCA. If set to false, the CacheSemaphore does nothingWork to be done
WithCache(ITokenCache)
which binds both app and user token cache. CCA only (?)[ ] Default in memory cache serializer based on Wilson LRU cache for ALL cca flows.[ ] LRU cache to utilize the SuggestedExpiration that is available on the NotificationArgs to optimize eviction[ ] LRU cache to default to 4GB of spaceThe text was updated successfully, but these errors were encountered: