Reputation: 815
I'm creating creating cookie in PageB and while click PageA link here it is redirecting to PageA. But I'm not seeing cookie in PageA. I'm not sure what I'm missing here.
[HttpGet]
public ActionResult PageA()
{
if (Request.Cookies["bCookie"] != null) {
//code
}
return ActionResult(View(PageA));
}
[HttpPost]
public ActionResult PageB(Model bCookieM)
{
HttpCookie bCookie= new HttpCookie("bCookie");
bCookie.Value = bCookieM.ToString();
Response.Cookies.Add(bCookie);
return View(PageB);
}
Upvotes: 1
Views: 972
Reputation: 32713
From the docs: If you do not set the cookie's expiration, the cookie is created but it is not stored on the user's hard disk. Instead, the cookie is maintained as part of the user's session information. When the user closes the browser, the cookie is discarded.
To make the cookie persistent (for 24 hours). Do something like this:
Response.Cookies["userName"].Value = userName;
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
HttpCookie bCookie = new HttpCookie("bCookie");
bCookie.Value = bCookieM.ToString();
bCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(bCookie);
Upvotes: 2