Yannik here Yannik here - 1 year ago 129
ASP.NET (C#) Question

Why I cannot delete a cookie?

I have following code in my


public ActionResult MyPage()
if (HttpContext.Request.Cookies["User"] == null)
//Create cookie
return RedirectToAction("MyPage", "Home");
string User = HttpContext.Request.Cookies["User"].Value;
foreach (var user in Data.MyUsers)
if (user.username == User)
//Do some stuff
return View("MyPage");
//HttpContext.Response.Cookies.Remove("User"); works not sorry ;(
return RedirectToAction("MyPage", "Home");

Problem: The application don’t delete the cookie (why ever) and run in a loop because the application doesn’t find the user in the data. (The problem is not that the application doesn’t find the user, the problem is the cookie-issue)

Question: How I can fix this issue because in another project of me I works fine. I copy and pasted the code from my other project in my new project, but in the new project it will not work. Yes I use the same version of the asp.net framework :)

Hope you can help me… I’m at the end of my knowhow about c#....

Thank you very much.

Answer Source

Use Expire Property, and set negative value to it, this will expires the cookie i.e in your word this will removed from cookies list.

String cookieName = Request.Cookies["User"].Name;
HttpCookie userCookie = new HttpCookie(cookieName);
userCookie.Expires = DateTime.Now.AddDays(-1);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download