Asp.net How To Remove Cookie
Do you want to remove or delete cookie using Asp.net? Here is a trick about how we can remove cookie from client. Cookies get stored at browser. You can’t delete them using Asp.net code. However the trick is, set expiration date before today’s date can tell browser to remove cookie. Let’s see example in Asp.net with C#.net and VB.net.
C#.net code to remove cookie.
1. First we need to detect whether cookie exists in request or not.
2. If cookie exists, set it’s expiration date to past.
3. Add new expiration date to cookie in response.
public partial class Demo : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (Request.Cookies["username"] != null)
HttpCookie cookie = new HttpCookie("username");
cookie.Expires = DateTime.Now.AddDays(-1);
VB.net Remove Cookie.
In Asp.net with Vb.net you can remove cookie at client side by adding expiration date prior to today's date. As I have already mentioned, you can't delete the cookie directly. It's browser responsibility to delete cookie.
Public Partial Class Demo
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Request.Cookies("username") IsNot Nothing Then
Dim cookie As New HttpCookie("username")
cookie.Expires = DateTime.Now.AddDays(-1)
You can set cookies for domain or specific to page. To set page specific cookie, use Path properties of HttpCookie instance.