title: Cookie not being persisted in MVC
body: <p>I am having a problem where a cookie I am setting is being lost directly after RedirectToAction() is called. Is something going on behind the scenes that invalidates the current request and creates a new one that causes the cookie to be lost before it has been persisted to disk? </p>
  
  <p>I understand that if you want data to be available after the redirect you need to use TempData, but is that the same for cookies?  If so, doesn't that seem ugly to have to store the cookie value in TempData and then write the cookie later?</p>
  
  <p><b>Update:</b></p>
  
  <p>I just realized that the cookie is lost at the end of the request, it doesn't matter if I call RedirectToAction(). So now the question is why wont the cookie persist accros two requests? (I update the code below to show what I am doing now)</p>
  
  <pre><code>public ActionResult DoSomething()
  {
     Response.Cookies["SomeCookie"].Value = "Jarified";
     Response.Cookies["SomeCookie"].Expires = DateTime.UtcNow.AddDays(3);
  
     return View("SomeView");
  
  }
  </code></pre>
  
  <hr>
  
  <p><b>Update</b></p>
  
  <p>I created a new MVC project using the default template. I modified the HomeController/Index action to have the code below. The first time I hit the view "Cookie Not Found" is printed as expected. However, every subsequent time the same message is printed.  If I remove the line that sets the expiration date then everything works just fine. I guess the real question here is why does making the cookie persistent cause the browser to throw it away? Is there a trick to making cookies persistent in MVC?</p>
  
  <pre><code>    public ActionResult Index()
      {
          HttpCookie cookie = Request.Cookies["temp"];
          if (cookie == null)
          {
              ViewData["Message"] = "Cookie Not Found";
              Response.Cookies["temp"].Value = "Welcome to ASP.NET MVC!";
              Response.Cookies["temp"].Expires = DateTime.UtcNow;
          }
          else
          {
              ViewData["Message"] = cookie.Value;
          }
          return View();
      }
  </code></pre>