Asp.net How To Expire a Cookie

Asp.net How To Expire a Cookie

You want to set expiry date of cookie. Here are asp.net C# and Vb.net code samples.

Asp.net C# Set Cookie expires.

protected void Page_Load(object sender, EventArgs e)
{

HttpCookie UserNameCookie = new HttpCookie("UserName");
UserNameCookie.Value = "Satalaj";
UserNameCookie.Expires.AddDays(1);

Response.Cookies.Add(UserNameCookie);

}

The cookie created in above example will expire in 1 day. You can also add TimeSpan, which will help you to expire the cookie in a minute or second.

Asp.net VB.net cookie expiry.

Protected Sub Page_Load(sender As Object, e As EventArgs)

Dim UserNameCookie As New HttpCookie("UserName")
UserNameCookie.Value = "Satalaj"
UserNameCookie.Expires.AddDays(1)

Response.Cookies.Add(UserNameCookie)

End Sub

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5