Delete browser cookie using C#.Net,Asp.Net


In my previous article I have explained about How to Add httpcookie using In this article I am going to explain about how to delete the browser cookies using C#.Net code.
You cannot directly delete a cookie on a user's computer. However, you can direct the user's browser to delete the cookie by setting the cookie's expiration date to a past date. 

To assign a past expiration date on a cookie

1.      Determine whether the cookie exists, and if so, create a new cookie with the same name.
2.      Set the cookie's expiration date to a time in the past.
3.      Add the cookie to the Cookies collection object.

Below is the method  used to delete the cookie.
protected void DeleteCookie(string cookiename)
    if (Request.Cookies[cookiename] != null)
        HttpCookie myCookie = new HttpCookie(cookiename);
        myCookie.Expires = DateTime.Now.AddDays(-1d);
Sample usage of the method is given below.

Do you like this article? Help us to improve. If you have any queries post it in comments. We will give you the solution.


Post a Comment

Online Casino