1

I'm having some really weird cookie issues on a website. It's an ASP.NET website, uses Umbraco 6.1.6 as its CMS but is mainly custom code and uses Forms Authentication for user login. It runs on IIS Server 2012 R2.

On successful registration, the user credentials are passed to the login method, which sets a cookie like this:

var authTicket = new FormsAuthenticationTicket(realUsername, false, 60);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
      {
               HttpOnly = true,
               Secure = FormsAuthentication.RequireSSL,
               Path = FormsAuthentication.FormsCookiePath,
               Domain = FormsAuthentication.CookieDomain,
               Expires = authTicket.Expiration
      };

HttpContext.Current.Response.Cookies.Set(cookie);

It then does a redirect, trying to go to wherever the user was previously on the site before registering.

return Redirect(redirectUrl);

From the redirected page, it checks the request cookie to see whether the user was logged in:

HttpCookie authCookie = HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);

Sometimes the authCookie contains the session data, and works for long periods of time..... and other times it is null - for equally long periods of time - so it only intermittently works.

This led me to believe that perhaps IIS is having trouble checking the request cookie on redirect - because the cookie has not yet reached the user's browser. But various docs and other SO posts tell me this should work when I am using a ResponseRedirect type redirect.

The bit where it gets weird is that when I've just registered but the server thinks I'm not logged in, I can look at the cookies for this "not logged in" page in Chrome dev tools, and it's true - there's no cookie set - however, when you click on the site's "Login" button from the "not logged in" page... which forces it to check whether the user is logged in again, this time "authCookie" contains cookie data. Right after Chrome told me this data was not present when I submitted the page! The cookie then reappears in dev tools when the "logged in" page is returned.

At this point, I don't know whether it's that I can't trust Chrome dev tools, or it's that there's some odd browser caching / IIS caching going on, or what. I tried looking up cookie / redirection issues and they only seem to come up with newer versions of IE.

I've tried closing the browser, closing visual studio, and that doesn't seem to have a direct impact.

One last bit of information - I read in one post here that there are sometimes issues when the registration / login is performed on one thread but the code after registration is performed on another thread. I checked the thread debugger and found that indeed the registration / login were being performed on different threads... but this was observed when it was working.

The most frustrating part is when it suddenly starts working for a few hours - and there's seemingly nothing I can do to break it again to try out different things. (Which is where I am now! I wanted to at least try it in different browsers...)

tenshi_a
  • 181
  • 2
  • 10

0 Answers0