Reputation: 634
I see that I remove the cookie in debug right after I use remove method of HttpContext.Request but when I redirect to another action I can still get cookie value. Why is that happening ?
public class LoginController : Controller
{
public ActionResult Logout()
{
HttpContext.Request.Cookies.Remove(Constants.User);
//I see that HttpContext.Request.Cookies[Constants.User] returns null in this line.
return RedirectToAction("Login", "Login");
}
public ActionResult Login()
{
var userCookie = HttpContext.Request.Cookies[Constants.User];
// But HttpContext.Request.Cookies[Constants.User] is not null here. I get the value from it.
if (userCookie != null)
return RedirectToAction(Constants.MainPage, Constants.MainPage);
else
return View();
}
}
Upvotes: 0
Views: 519
Reputation: 19828
To remove cookie you have to send it with an expiration date set to now or previous date. Removing it using HttpContext.Request.Cookies.Remove(Constants.User);
just remove it from the collection, but it still exists in a client browser.
For example instead of using Remove
function use:
Response.Cookies[Constants.User].Expires = DateTime.Now.AddDays(-1);
Upvotes: 3