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 }}
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
When using the implicit flow, the AspNetCore Identity sliding cookie functionality does not work. On the authorize endpoint, the cookie is not refreshed when necessary.
Cause
When rendering the response of the authorize endpoint, the function AddClientAsync is called.
protectedasync Task ProcessResponseAsync(HttpContextcontext){if(!Response.IsError){// success response -- track client authorization for sign-out//_logger.LogDebug("Adding client {0} to client list cookie for subject {1}", request.ClientId, request.Subject.GetSubjectId());await _userSession.AddClientIdAsync(Response.Request.ClientId);}await RenderAuthorizeResponseAsync(context);}
This method in DefaultUserSession.cs adds the ClientId to the AuthenticationTicket that is used by the Cookie middleware. This is done by calling the method SignInAsync with the properties of the existing ticket, which includes the expiration time.
publicvirtualasync Task AddClientIdAsync(stringclientId){if(clientId==null)thrownew ArgumentNullException(nameof(clientId));await AuthenticateAsync();
Properties?.AddClientId(clientId);await UpdateSessionCookie();}privateasync Task UpdateSessionCookie(){await AuthenticateAsync();if(Principal==null||Properties==null)thrownew InvalidOperationException("User is not currently authenticated");varscheme=await HttpContext.GetCookieAuthenticationSchemeAsync();await HttpContext.SignInAsync(scheme, Principal, Properties);}
However the AspNetCore Identity CookieAuthenticationHandler will not refresh the cookie in a scope where the function SignInAsync is called. But since the existing ticket properties are passed to the function, the expire of the cookie is not changed. In the implicit flow where the client only calls the authorize endpoint, the cookie is never refreshed.
In IdentityServer4 v3.1 this does work because the cookie is only updated when the ClientId is not already in the cookie. In v4 the ClientId is only added to the properties when it is not already present, but the cookie is always recreated.
protectedvirtualasync Task FinishResponseAsync(){// Only renew if requested, and neither sign in or sign out was calledif(!_shouldRefresh||_signInCalled||_signOutCalled){return;}varticket= _refreshTicket;
...}
A possible solution is to again only recreate the cookie when a new ClientId is added and not every time. This is also currently our workaround with a custom IUserSession implementation.
Other solution could be to clear the expiration timestamp from the ticket properties before calling SignInAsync. However this should only be done when sliding cookies are enabled.
The text was updated successfully, but these errors were encountered:
Issue
When using the implicit flow, the AspNetCore Identity sliding cookie functionality does not work. On the authorize endpoint, the cookie is not refreshed when necessary.
Cause
When rendering the response of the authorize endpoint, the function
AddClientAsync
is called.This method in
DefaultUserSession.cs
adds the ClientId to theAuthenticationTicket
that is used by the Cookie middleware. This is done by calling the methodSignInAsync
with the properties of the existing ticket, which includes the expiration time.However the AspNetCore Identity
CookieAuthenticationHandler
will not refresh the cookie in a scope where the functionSignInAsync
is called. But since the existing ticket properties are passed to the function, the expire of the cookie is not changed. In the implicit flow where the client only calls the authorize endpoint, the cookie is never refreshed.In IdentityServer4 v3.1 this does work because the cookie is only updated when the ClientId is not already in the cookie. In v4 the ClientId is only added to the properties when it is not already present, but the cookie is always recreated.
Equivalent code in IdentityServer4 v3.1:
The relevant code snippets of CookieAuthenticationHandler.
_signInCalled
is set to true, which causes the refresh logic to return immediately.A possible solution is to again only recreate the cookie when a new ClientId is added and not every time. This is also currently our workaround with a custom
IUserSession
implementation.Other solution could be to clear the expiration timestamp from the ticket properties before calling
SignInAsync
. However this should only be done when sliding cookies are enabled.The text was updated successfully, but these errors were encountered: