CookieUseSlidingExpiration do not renew the cookie properly
From @daboul on September 14, 2016 8:20
Hi. I'm a little bit stuck here, any help would be greatly appreciated (first post on SO, sorry if I'm doing something wrong).
I managed to use the ASP.NET Core CookieAuthentication to create different cookie per route. If I call http://host/API/moduleName1/login it will create a authentication cookie with a path set to /API/moduleName1 that will be used to authenticate a user only for calls under this path. To do that, I plugged myself on the SigningIn event and hacked the cookie path.
My Configure() code:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "...",
CookieName = "...",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
CookieHttpOnly = false,
ExpireTimeSpan = userSettingsConfig.Value.CookieExpireTimeSpan,
SlidingExpiration = userSettingsConfig.Value.CookieUseSlidingExpiration,
Events = new CookieAuthenticationEvents
{
OnSigningIn = CustomAuthenticator.SigningInAsync,
OnSigningOut = CustomAuthenticator.SigningOutAsync,
}
});
And my customisation:
public static class CustomAuthenticator
{
public static async Task SigningInAsync(CookieSigningInContext context)
{
string controller = context.HttpContext.GetRouteValue("controller").ToString();
string moduleName = context.HttpContext.GetRouteValue("moduleName").ToString();
context.CookieOptions.Path = context.HttpContext.Request.PathBase + "/" + controller + "/" + moduleName;
}
public static async Task SigningOutAsync(CookieSigningOutContext context)
{
string controller = context.HttpContext.GetRouteValue("controller").ToString();
string moduleName = context.HttpContext.GetRouteValue("moduleName").ToString();
context.CookieOptions.Path = context.HttpContext.Request.PathBase + "/" + controller + "/" + moduleName;
}
}
It worked well for a while, but then, playing with the cookie ExpireSpan set to 1 minute I discovered something annoying.
When I log myself in, I get something very logical:a session cookie with a properly set cookie path: '/API/PLENG'
But after a while, if I remain active, because of the CookieUseSlidingExpiration set to true, the middleware decide to renew my cookie and here is what I get: A second cookie with a cookie path set to '/'. Instead of renewing my cookie properly, the middleware created a new one that is completely messing with my design.

So I get that it's kind of my fault for having hacked the system, but I can't live without this Sliding Expiration mechanism and I'm left with no idea right now.
an someone tell me how I should have done that in the first place (instead of hacking the cookie path)? Would you call this Sliding Expiration = true behavior buggy here?
Thanks.
Copied from original issue: aspnet/Home#1756
This seems like the sort of issue that should be solved at the authorization policy layer, not at the cookie layer. @HaoK ?
Ping @Tratcher can you add some further thoughts to this?
The path is not available during a refresh. the initial path is set from CookieAuthOptions.Path but then you're overriding it in an event. The value is sent to the client but they do not send it back on the request.
The best we can do here is add an event for sliding expiration renewals, but it's unclear how you would preserve the paths for that code path.
Also note that you can't depend on the client browser to properly enforce Path. Anything security critical must be encoded in the cookie itself and validated on the server.
I understand your answer and I do think an event on the renewal would make a lot of sense here. As you suggest, I'll make sure the path gets also included in the cookie so that I can check the path hasn't been tampered with.
This event would go in FinishResponseAsync. Recommend this go in backlog until 2.0, adding events right now requires breaking interface changes.
Closing because we are not planning to implement this.