How to destroy a cookie C#.NET

February 5, 2008

Basically have to set its expire date to the past! (the cookies we created for our site lasted a day so -1d was enough for this example)

public void DestroyCookie(string cookieName)

    {

        if (HttpContext.Current.Request.Cookies[cookieName] != null)

        {

            HttpCookie myCookie = new HttpCookie(cookieName);

            myCookie.Expires = DateTime.Now.AddDays(-1d);

            HttpContext.Current.Response.Cookies.Add(myCookie);

        }

    }