Should clear session auth cookie if cache is missing account
See original GitHub issuefrom @onovotny and copied from https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/issues/240
In the Microsoft.Identity.Web library, the system should automatically clear (RejectPrincipal()
) the auth cookie if the corresponding account entry is missing from the token cache. This can happen if the cache expired, if it was a memory cache and the server bounced, etc.
The issue is that the system is now in an inconsistent state, where the user is considered logged-in, but any operations to call API’s won’t succeed because there’s no token cache, no refresh token, etc.
I’ve worked around this here: https://github.com/dotnet-foundation/membership
In order to do so, I had to make some changes to ITokenAquisition
and the MsalAbstractCacheProvider’s GetCacheKey method.
ITokenAcquisition
needed a method to check for a user’s token cache:
https://github.com/dotnet-foundation/membership/blob/97b75e30e50aab76bfa5a21f1ab88bf31ae66da4/Microsoft.Identity.Web/TokenAcquisition.cs#L406-L426
In there, it takes the CookieValidatePrincipalContext
to get the incoming ClaimsPrincpal as HtttpContext.User is not yet set at that point. It stores it in the HttpContext.Items via the StoreCookieValidatePrincipalContext
extension method (used later by the GetCacheKey method so it can derive the appropriate key):
https://github.com/dotnet-foundation/membership/blob/97b75e30e50aab76bfa5a21f1ab88bf31ae66da4/Microsoft.Identity.Web/TokenCacheProviders/MsalAbstractTokenCacheProvider.cs#L68-L69
Finally, the CookieAuthenticationOptions
needs to be configured to check for and reject the incoming principal (this could/should be moved into the main IdentityPlatform AddMsal extension methods):
https://github.com/dotnet-foundation/membership/blob/97b75e30e50aab76bfa5a21f1ab88bf31ae66da4/Membership/Startup.cs#L110-L123
I can submit these changes as PR if you’re in agreement with these changes.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:60 (1 by maintainers)
@jennyf19 @clairernovotny
Here is the design I propose for this issue:
Add a new method
.WithForceLoginWhenEmptyCache()
on theMicrosoftIdentityAppCallsWebApiAuthenticationBuilder
to force the users to login when there is a session cookie, but no account in the cache (for instance because the cache is an in memory token cache and the application was restarted).This would be an opt-in method, used like this:
It’s implementation could be something like the following:
and with
Alternatively
AccountDoesNotExitInTokenCache
could be a bool property surfaced onMicrosoftIdentityWebChallengeUserException
@clairernovotny : I provided the work around and tested it.
In Startup.cs:
And then (inspired by what you had)