I am facing a problem. I have created two sessions:
Session["userid"] = UserTbl.userid;
Session["userType"] = UserTbl.type;
I know how to remove sessions using Session.clear()
. I want to remove the session "userType".
How do I remove a specific session?
There are many ways to nullify session in ASP.NET. Session in essence is a cookie, set on client's browser and in ASP.NET, its name is usually ASP.NET_SessionId
. So, theoretically if you delete that cookie (which in terms of browser means that you set its expiration date to some date in past, because cookies can't be deleted by developers), then you loose the session in server. Another way as you said is to use Session.Clear()
method. But the best way is to set another irrelevant object (usually null
value) in the session in correspondance to a key. For example, to nullify Session["FirstName"]
, simply set it to Session["FirstName"] = null
.
A single way to remove sessions is setting it to null;
Session["your_session"] = null;
you can use Session.Remove() method; Session.Remove
Session.Remove("yourSessionName");
HttpContext.Current.Session.Remove("sessionname");
it works for me
Session.Remove("name of your session here");
Source: Stackoverflow.com