Remove Cookie in ASP.NET

I was working on the Cookies using the HTTPCookie class in ASP.NET. Though it is simple to add cookie and use them in the program but I really struggle to remove the cookies. I tried initially

  1. Get the reference to the cookie.
  2. Use the Response.Cookies.Remove method to remove the cookie.

I expected the API to behave as per the name i.e. delete the cookie but to my utter shock it didn’t work. After long search I found the solution. The key was to make the cookie expire instead of removing it from the collection

Response.Cookies[“MY_COOKIE”].Expires = DateTime.Now;

This works perfectly for me.

 

Advertisements