title: Cookies not saved between browser sessions on iOS Safari
body: <p>I have an MVC 4 website where a user can login and I save a cookie with their session information so they don't have to login again.</p>
  
  <pre><code>public void SetCookie(HttpCookie cookie)
  {
      HttpContext.Current.Response.Cookies.Set(cookie);
  }
  </code></pre>
  
  <p>This works on desktop devices, but when I run this on iOS, it doesn't work 100% of the time. If I leave at least 1 page open in the mobile browser (either Chrome or Safari), and navigate back to my site, my session cookie is found and I don't have to login. But if I close ALL windows of that browser, then the next time I navigate back to my site, the session cookie is not found. I'm setting a 1 year duration/expiration on my cookie so it's not expiring.</p>
  
  <p>The only thing I've found so far is that this is a bug in .NET 4.0 and is fixed in .NET 4.5. I believe I'm already running .NET 4.5, by looking at my *.csproj and the TargetFrameworkVersion element:</p>
  
  <pre><code>&lt;Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /&gt;
    &lt;PropertyGroup&gt;
      &lt;Configuration Condition=" '$(Configuration)' == '' "&gt;Debug&lt;/Configuration&gt;
      &lt;Platform Condition=" '$(Platform)' == '' "&gt;AnyCPU&lt;/Platform&gt;
      &lt;ProductVersion&gt;
      &lt;/ProductVersion&gt;
      &lt;SchemaVersion&gt;2.0&lt;/SchemaVersion&gt;
      &lt;ProjectGuid&gt;{xxxxxxxxxxxxxxxxxx}&lt;/ProjectGuid&gt;
      &lt;ProjectTypeGuids&gt;{xxxxxxxx};{xxxxxxxxxxxxx};{xxxxxxxxxxxxxxxx}&lt;/ProjectTypeGuids&gt;
      &lt;OutputType&gt;Library&lt;/OutputType&gt;
      &lt;AppDesignerFolder&gt;Properties&lt;/AppDesignerFolder&gt;
      &lt;RootNamespace&gt;MyApp&lt;/RootNamespace&gt;
      &lt;AssemblyName&gt;MyApp&lt;/AssemblyName&gt;
      &lt;TargetFrameworkVersion&gt;v4.5&lt;/TargetFrameworkVersion&gt;
      &lt;MvcBuildViews&gt;false&lt;/MvcBuildViews&gt;
      &lt;UseIISExpress&gt;true&lt;/UseIISExpress&gt;
      &lt;IISExpressSSLPort /&gt;
      &lt;IISExpressAnonymousAuthentication /&gt;
      &lt;IISExpressWindowsAuthentication /&gt;
      &lt;IISExpressUseClassicPipelineMode /&gt;
    &lt;/PropertyGroup&gt;
  </code></pre>
  
  <p>Is this a bug with .NET (or MVC), or is it my coding? Do I need to upgrade to MVC 5 (instead of the original recommendation of upgrading to .NET 4.5)?</p>
  
  <p>This has really been bugging me since the majority of the traffic to my site in the next coming months will be from mobile users, and I'll probably lose some of them if they have to keep logging in everytime (I know I hate having to login on a mobile device...)</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Btw - Here's another page on the same subject: <a href="https://stackoverflow.com/questions/4158550/asp-net-forms-authentication-when-using-iphone-uiwebview">Asp.Net Forms Authentication when using iPhone UIWebView</a></p>
  
  <p>And I've also tried implementing this and it didn't work either:</p>
  
  <p><a href="http://www.bloggersworld.com/index.php/asp-net-forms-authentication-iphone-cookies/" rel="noreferrer">http://www.bloggersworld.com/index.php/asp-net-forms-authentication-iphone-cookies/</a></p>
  
  <p>And the above includes Scott Hanselmans suggested fix:</p>
  
  <p><a href="http://www.hanselman.com/blog/FormsAuthenticationOnASPNETSitesWithTheGoogleChromeBrowserOnIOS.aspx" rel="noreferrer">http://www.hanselman.com/blog/FormsAuthenticationOnASPNETSitesWithTheGoogleChromeBrowserOnIOS.aspx</a></p>
  
  <p>Here's my cookie creation code, in case it helps:</p>
  
  <pre><code>private void CreateAndSetAuthenticationCookie(int loginId, string username)
      {
          HttpCookie cookie = CreateAuthenticationCookie(loginId, username);
  
          _cookieService.SetCookie(cookie);
      }
  
      private HttpCookie CreateAuthenticationCookie(int loginId, string username)
      {
          string userData = string.Format("loginId:{0},username:{1}", loginId, username);
          var ticket = new FormsAuthenticationTicket(loginId, username, DateTime.Now, DateTime.Now.AddYears(1), false, userData, FormsAuthentication.FormsCookiePath);
          string encryptedTicket = FormsAuthentication.Encrypt(ticket);
  
          return new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
      }
  </code></pre>
  
  <p>One thing that I pointed out in my comments, I have "PersistentCookie" set to false....not sure if that makes a difference, I'll do more research.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>First of all, the PersistentCookie is set to true now, and that did not have any changes on the behavior.</p>
  
  <p>One of the commentors below suggested that I run Fiddler while accessing the website from iPhone. After going through the very short and easy steps to get that setup, here's what I found out.</p>
  
  <p>The first request I tried pointed out (I believe) what the actual problem is. When I examined that first request, iOS Safari is sending a DNT header. Having no idea what that was, I looked it up and it's a "Do Not Track" header.</p>
  
  <p>Next, I went and checked my Safari settings to turn that off. Guess what, it's already off. Why the hell is Safari (iOS) sending a DNT header when the setting (Settings -> Safari -> Do Not Track) is not set? Also, the Block Cookies, which is right below it, is set to "Never".</p>
  
  <p>After getting frustrated with that, I went to check on Chrome for iOS to see if that still doesn't work. IT WORKS! From what I can tell, I'd close all tabs, try again, close all tabs, then kill the browser, and it still works. Hooray!</p>
  
  <p>Now, I need to figure out why iOS Safari is sending the DNT header...</p>