Loosing a Cookie when switching from http:// to https://
I recently changed the code in my online West Wind Web Store to specifically write the one cookie I use in the app to a specific path so that there's no interference with other virtuals also running the store. I thought this was necessary to run successfully with multiple stores.
Changing Cookie paths is easy enough and since I use a common cookie class this was easy to do in one place by just making sure that the app writes out the cookie with a common path. By default I write out the Application path with something like this:
public void WriteCookie(string Value, bool NonPersistent)
{
HttpCookie Cookie = new HttpCookie(CookieName,Value);
Cookie.Path = HttpContext.Current.Request.ApplicationPath + "/";
if (!NonPersistent)
Cookie.Expires = DateTime.Now.AddMonths(CookieTimeoutInMonths);
HttpContext.Current.Response.Cookies.Add(Cookie);
}
I tested this locally and it worked just dandy, so this seemed like an easy enough fix for multiple store cookie overlap.
But surprise, surprise I didn't test with HTTPS/SSL. The live store switches from http:// to https:// once the order is submitted and with the application path scoped cookie, and it turns out both IE and FireFox loose the cookie in this transition. If the user was previous logged in to his profile the profile is lost.
Worse, even trying to reconnect the cookie under https seems to fail – it looks like under https cookie paths don't work. Everytime I try to reconnect to a new cookie after assigning it is lost.
The code starts working on the live site as soon as I comment out the Path assignment.
// Cookie.Path = HttpContext.Current.Request.ApplicationPath + "/";
Put the above code back in and - boom - it fails to work again.
I took a look at the cookie files and the cookies look just fine. The requests go from http:// to https:// without anything code manipulating the cookie – the cookie just disappears.
WebStoreUser
12139eff
www.west-wind.com/wwstore/
1024
4039391232
30053033
3782347520
29759130
*
Now, lest you think I track my profile with a cookie – no, the cookie is used only to attach to the profile once. The cookie is a permanent cookie I write out so users can automatically reattach to their profile when they return to the site. After the initial Cookie lookup a Session variable tracks the users customer id that points at the customer record. But of course when the cookie goes, the ASP.NET Session Cookie goes with it so the customer link is lost.
I'm not quite sure what the heck is happening because don't have a tool to examine the secure https content (Fiddler doesn't work with https) to see where the cookie gets dropped.
It works now having changed back to the root path, but anybody have any idea why a virtual path would fail in https?
Other Posts you might also like
The Voices of Reason
# re: Loosing a Cookie when switching from http:// to https://
# re: Loosing a Cookie when switching from http:// to https://
# re: Loosing a Cookie when switching from http:// to https://
# re: Loosing a Cookie when switching from http:// to https://
# re: Loosing a Cookie when switching from http:// to https://
We have the same issue, But the difference is it is not happening when it is switching HTTP and HTTS but when user comes from www. and without www. Do you think by giving / to the Application Path of cookie can solve this issue ?
HTTP and HTTPS is pointing to the same folder so we no issues with path.
Many thanks for the suggestion
Kind Regards
Sunil Warrier
# re: Loosing a Cookie when switching from http:// to https://
# re: Loosing a Cookie when switching from http:// to https://
@Sunil, for cookies to work on both http://my-domain.com and http://www.my-domain.com you need to set the cookie domain to ".mydomain.com" so that it will work cross-domain.
# re: Loosing a Cookie when switching from http:// to https://
@Rick, looks like the issue you have/had is due to the nature of cookies themselves... secure cookies are intended only to pass across an HTTPS connection and, sensible, browsers will enforce this.
Apologies if this comment is waaaay to late :-)
# re: Loosing a Cookie when switching from http:// to https://
Incidentally, everything was working fine on my test environment where both applications were under HTTP. Any clues ???
# re: Loosing a Cookie when switching from http:// to https://
oh, which Date was this, nevermind ...
The Problem ist not that Browsers do mistakes. The Problem is, that https and http neither are the same server nor must contain the same domain. Netscape in it's erlier versions of Navigator extended this behaviour by deciding between ports!
So it's in charge of the programmer to tell the new server the correct session id to use a shared session storage.
I will do by session. if the session has been called and it's the first time the user has requested i will forward to either http or https to set the id. Session-Surfing in a wanted manner. If it's the first time the user request and get forwarded the foreign server and the identicaly software will know through a GET Parameter that he only have to set the id an return to the server which have to be configured. Otherwise it will end up in an infinite loop.
Problems for now and for all security dependent changes in future gone ...
# re: Loosing a Cookie when switching from http:// to https://
Did any of you able to find the solution for losing cookie when switching between http and https.
I have the same problem.
In my application, only the login page has https(SSL). I have my cookie generated during the login process, and when the user logs in, cookie is not passed when the request changes to http.
Since i do not have SSL enabled in my test region, it did not occur to me untill i moved the code to production.
This is how i set the cookie:
CookieUtils.setCookie(httpServletRequest,httpServletResponse,"SSO",username,-1,"/");
Please share your thoughts.
# re: Loosing a Cookie when switching from http:// to https://
does explicitly setting the 'Secure' bool to true on the HTTPCookie help anything?
does the name on the cert exactly match the server name in the url?