title: Browser occasionally losing HttpCookie for authentication after postback and redirect
body: <p>This has been a nagging issue for some time, but very sporadic and difficult to isolate.</p>
  
  <p>From time to time, browsers that have authenticated on a web application, have been open for a while, have logged in and out of the same web application multiple times, have multiple tabs, are pretty much any browser (Chrome, IE, Firefox, Safari), and seemingly at random, lose their ability to retain an AuthCookie after being set and followed by a redirect. Closing the browser and starting a new session resolves the issue, as does opening up a different browser and attempting to authenticate.</p>
  
  <p>Our team uses forms authentication for all of our websites and web application.  This is a pretty typical setup where a login form is displayed, the user enters credentials and a cookie is set on the click event of the postback, then a redirect occurs to the same page where the cookie is then referenced and used to complete authentication.</p>
  
  <p>In this situation</p>
  
  <pre><code>FormsAuthentication.FormsCookieName = ".WebAuth"
  </code></pre>
  
  <p>Within Event:</p>
  
  <pre><code>    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, Username, DateTime.Now, DateTime.Now.AddMinutes(SessionTimeout), false, Username);
      HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
      Response.Cookies.Add(faCookie);
  Response.Redirect(Request.RawUrl, true);
  </code></pre>
  
  <p>After the redirect, on PreInit:</p>
  
  <pre><code>HttpCookie authCookie = Request.Cookies[cookieName];
  </code></pre>
  
  <p>At this point, the authCookie variable is typically not null, but in these isolated circumstances that I've outlined above, the cookie comes back null after the redirect.</p>
  
  <p>This happens very randomly, sometimes weeks before affecting one of our developers. As I said, restarting the browser resolves the issue.</p>
  
  <p>Today I had it happen on our dev server while using Chrome. I had logged into the application, allowed the application to session timeout, and then attempted to login again.  The attempted login then failed to set the cookie. I remotely attached Visual Studio to the process on the server to begin debugging.  The entire time I could step through my code, even deploy new code versions to the server with updates, restart the app, restart IIS on the server, attach and reattach to the project, and the issue persisted in Chrome.  In Firefox, I was able to authenticate without issue.</p>
  
  <p>From Chrome, the login would validate, attempt to set a Response Cookie as outlined above.  Prior to redirect, I could see the properly set Response Cookie, as well as its counterpart in the Request Cookies. However, on each redirect after a seemingly successful login, the Response and Request Cookie are gone.</p>
  
  <p>I enabled Trace on the application to view the cookie collection:</p>
  
  <p>There is a .WebAuth in the Request Cookies Collection, as well as ASP.NET_SessionId and several ASPSESSIONIDxxxxxxxx, but when the page loads, only the ASP.NET_SessionId and  ASPSESSIONIDxxxxxxxx cookies are available in the Request.Cookies scope, no sign of the .WebAuth.  However, in the page's Trace information after render, there multiple .WebAuth cookies listed, it is just that the page seems to have no access to them.</p>
  
  <p>Primarily, on a working version after authentication there is both a .WebAuth Response and Request Cookie in the page's Trace info.  But on a non functioning browser window, the Response Cookie is absent.</p>
  
  <p>Has anyone else had any experience with this? It is such a nagging issue, and so sporadic, but I would love to be able to resolve it. My concern is that it may be affecting users and we would have no knowledge since the description of the issue is so convoluted.</p>