How do you delete a cookie in ASP.NET?
In ASP.NET, you can delete a cookie by setting its expiration date to a date in the past. When a cookie expires, it is automatically removed from the user's browser. To delete a cookie in ASP.NET, you can use the following code:
// Assuming the cookie name is "MyCookie"
HttpCookie myCookie = new HttpCookie("MyCookie");
myCookie.Expires = DateTime.Now.AddDays(-1); // Set expiration date to the past
Response.Cookies.Add(myCookie);
In this code, we create a new instance of the HttpCookie class with the same name as the cookie you want to delete. We then set the Expires property of the cookie to a date in the past (in this case, one day ago). Finally, we add the cookie to the Response.Cookies collection to send it to the client with the updated expiration date. When the client receives this response, it will remove the cookie from its browser.