Microsoft: how to delete a cookie
You cannot directly delete the cookie on the user's computer. However, you can direct the user's browser to delete cookies by setting the cookie expiration date to a past date. The next time the user makes a request for a page in the domain or path that sets the cookie, the browser determines that the cookie has expired and deleted it.
To set a cookie expiration date
- Determine if a cookie exists in the request, and if so, create a new cookie with the same name.
- Set the cookie expiration date to the past.
- Add a cookie to the Cookies Response collection object.
The following code example shows how to set an expiration date in a cookie.
if (Request.Cookies["UserSettings"] != null) { HttpCookie myCookie = new HttpCookie("UserSettings"); myCookie.Expires = DateTime.Now.AddDays(-1d); Response.Cookies.Add(myCookie); }
Note. Calling the Remove method from the Cookies collection removes the cookie from the collection on the server side, so the cookie will not be sent to the client. However, this method does not remove the cookie from the client if it already exists.
source share