CookieUseSlidingExpiration do not renew the cookie properly
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.
This issue was moved to aspnet/Security#991