How to Kill A Session or Session ID (ASP.NET/C#) How to Kill A Session or Session ID (ASP.NET/C#) asp.net asp.net

How to Kill A Session or Session ID (ASP.NET/C#)


The Abandon method should work (MSDN):

Session.Abandon();

If you want to remove a specific item from the session use (MSDN):

Session.Remove("YourItem");

EDIT: If you just want to clear a value you can do:

Session["YourItem"] = null;

If you want to clear all keys do:

Session.Clear();

If none of these are working for you then something fishy is going on. I would check to see where you are assigning the value and verify that it is not getting reassigned after you clear the value.

Simple check do:

Session["YourKey"] = "Test";  // creates the keySession.Remove("YourKey");    // removes the keybool gone = (Session["YourKey"] == null);   // tests that the remove worked


It is also a good idea to instruct the client browser to clear session id cookie value.

Session.Clear();Session.Abandon();Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-10);


Session.Abandon()

This marks the session as Abandoned, but the session won't actually be Abandoned at that moment, the request has to complete first.