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

Tags:

Asp.net How To Expire a CookieAsp.net How To Remove CookieAsp.net page specific cookieAsp.net Interview Questions for 2 years or 3 years experianceASp.net 2.0 step by step Membership Provider asp net image uploadWhat is web garden in Asp.net?ASP.net File HandlingSessions In AspnetAsp.net open new windowHack asp.net

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.