Search This Blog

Sunday, July 17, 2011

Destroy cookie using VB.NET and ASP.NET

This is a small function which allows you to destroy a cookie.
All that is needed is to pass the cookie name.  If the cookie is
found, it'll set it so that it has expired.

Public Shared Sub DestroyCookie(ByVal CookieName As String)
 Dim CookieObj As HttpCookie = HttpContext.Current.Request.Cookies("CookieName")
   
    If CookieObj IsNot Nothing Then
        CookieObj.Expires = DateTime.Now.AddDays(-1d)
        HttpContext.Current.Response.Cookies.Add(CookieObj)
       
    End If
End Sub


Just call this static function and.If you are calling it from a regular
class you may want to make it a non static method.  I had this is
a HttpHelper class, which I would just call by using
HttpHelper.DestroyCookie("mycookiename")

No comments:

Post a Comment