Search code examples
c#asp.netsessioncookiessession-cookies

How to delete cookies on an ASP.NET website


In my website when the user clicks on the "Logout" button, the Logout.aspx page loads with code Session.Clear().

In ASP.NET/C#, does this clear all cookies? Or is there any other code that needs to be added to remove all of the cookies of my website?


Solution

  • Try something like that:

    if (Request.Cookies["userId"] != null)
    {
        Response.Cookies["userId"].Expires = DateTime.Now.AddDays(-1);   
    }
    

    But it also makes sense to use

    Session.Abandon();
    

    besides in many scenarios.