You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
There should be an option to have TokenAcquisition be a singletonΒ #1
- [ ] bug report -> please search issues before submitting
- [ x] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)
There are scenarios, like using the Graph SDK, which require it to be a singleton. This is a request from the Graph SDK team (Darrel Miller).
Given Bogdan's analysis (below), this is safe to have a singleton.
What: proposed developer experience?
Provide an option in the configuration, which sets the TokenAcquisition service to be a singleton.
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
// missing lines here ..."SingletonTokenAcquisition" : false
},
// missing lines here ...
}
Proposed design:
Add a boolean SingletonTokenAcquisition in the MicrosoftIdentityOptions, by default this would be false.
Add a bool parameter in AddTokenAcquisition to specify if the token acquisition service should be a singleton or not (and use .AddScoped if it's not a singleton, and AddSignleton if this is a singleton)
Pass this parameter in methods calling AddTokenAcquisition, the value coming from the MicrosoftIdentityOptions.SingletonTokenAcquisition. Probably:
When new tokens arrive, re-read the cache and merge the new tokens
Steps 1 and 3 are guaranteed to be consistent because we use locks. The entire AcquireTokenSilent is not locked. So the whole thing might work from a consistency perspective, even
However, please note that using the same cache has performance implications:
a. Operations 1 and 3 above are probably executed in a critical section . This can result in high contention, especially under heavy load. This isn't a bug in MSAL per se, but we'll have to retest token cache web implementations. Some improvements can be made such as using separate read and write locks.
b. Token cache has not been perf tested for holding large number of entries. We've had bugs in the past around this, especially with around ADAL fallback mechanism.
c. Although not perf related, storing all tokens in the same cache is a security issue. If we ever introduce a bug in MSAL which would allow user A to fetch a token that does not belong to user A, today we are protected by the fact user A has its own cache. With a shared cache, this would be a major security incident.
All in all, this work item requires a lot of testing.
Then concern a. above is also not relevant. The locks are at the Token Cache level, so if you have a token cache for each account, contention is not an issue.
Activity
bgavrilMS commentedon Feb 18, 2020
Jean-Marc Prieur (@jmprieur) - I had a look at this scenario in more detail.
AquireTokenSilent does:
Steps 1 and 3 are guaranteed to be consistent because we use locks. The entire AcquireTokenSilent is not locked. So the whole thing might work from a consistency perspective, even
However, please note that using the same cache has performance implications:
a. Operations 1 and 3 above are probably executed in a critical section . This can result in high contention, especially under heavy load. This isn't a bug in MSAL per se, but we'll have to retest token cache web implementations. Some improvements can be made such as using separate read and write locks.
b. Token cache has not been perf tested for holding large number of entries. We've had bugs in the past around this, especially with around ADAL fallback mechanism.
c. Although not perf related, storing all tokens in the same cache is a security issue. If we ever introduce a bug in MSAL which would allow user A to fetch a token that does not belong to user A, today we are protected by the fact user A has its own cache. With a shared cache, this would be a major security incident.
All in all, this work item requires a lot of testing.
jmprieur commentedon Feb 18, 2020
Thanks Bogdan Gavril (@bgavrilMS)
about b and c, we don't store many accounts in a cache. We have one cache per account (the key is an account ID)
bgavrilMS commentedon Feb 18, 2020
Then concern a. above is also not relevant. The locks are at the Token Cache level, so if you have a token cache for each account, contention is not an issue.
jmprieur commentedon Feb 18, 2020
Thanks for confirming Bogdan Gavril (@bgavrilMS)
jmprieur commentedon May 26, 2020
This is illustrated by the following sample:
https://github.com/AzureAD/microsoft-identity-web/tree/master/tests/WebAppCallsMicrosoftGraph
jennyf19 commentedon Jun 1, 2020
In 0.1.4-preview release
Update AOT annotations (#3664)