Cookies not saved in MVC

I am having a problem where the cookie that I set is lost immediately after calling RedirectToAction (). Is something happening behind the scenes that invalidates the current request and creates a new one that causes the cookie to be lost before it is saved to disk?

I understand that if you want the data to be accessible after the redirect, you need to use TempData, but is it the same for cookies? If so, doesn’t it seem ugly to store the cookie value in TempData and then write the cookie later?

Update:

I only realized that the cookie was lost at the end of the request, it doesn't matter if I call RedirectToAction (). So now the question is why cookies save two requests? (I am updating the code below to show what I am doing now)

public ActionResult DoSomething()
{
   Response.Cookies["SomeCookie"].Value = "Jarified";
   Response.Cookies["SomeCookie"].Expires = DateTime.UtcNow.AddDays(3);

   return View("SomeView");

}

Update

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 got to the point of view, "Cookie Not Found" is printed, as expected. However, each subsequent moment the same message is printed. If I delete the line that sets the expiration date, then everything will be fine. My guess is that the real question is: why does persistence of the cookie cause the browser to drop it? Is there a trick to creating cookies in MVC?

    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();
    }
+5
3

,

Response.Cookies["temp"].Expires = DateTime.UtcNow;

, , cookie ( ).

,

Response.Cookies["temp"].Expires = DateTime.UtcNow.AddDays(3);

cookie expires cookie.

:

    public ActionResult Index() {
        HttpCookie cookie = Request.Cookies["temp"];
        if (cookie == null) {
            ViewData["Message"] = "Cookie Not Found";
            Response.Cookies["temp"].Value = "This is a cookie: Welcome to ASP.NET MVC!";
            Response.Cookies["temp"].Expires = DateTime.UtcNow.AddDays(3);
        } else {
            return RedirectToAction("Something");
        }
        return View();
    }

    public ActionResult Something() {
        HttpCookie cookie = Request.Cookies["temp"];
        ViewData["Message"] = cookie.Value;
        return View();
    }
+5

. !

http://stephenwalther.com/blog/archive/2008/07/08/asp-net-mvc-tip-15-pass-browser-cookies-and-server-variables-as-action-parameters.aspx

    var newCookie = new HttpCookie("myCookie", cookieValue);
    newCookie.Expires = DateTime.Now.AddDays(10);
    Response.AppendCookie(newCookie);
    return RedirectToAction("Index");
+2

cookie

Response.Cookies.Add(new HttpCookie("myCookie", "cookie value") 
    { Expires = DateTime.Now.AddDays(1)});
0

All Articles