38

I am facing a problem. I have created two sessions:

  1. Session["userid"] = UserTbl.userid;
  2. 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?

BryanH
  • 5,826
  • 3
  • 34
  • 47
Chintan
  • 2,768
  • 4
  • 27
  • 43

6 Answers6

84
Session.Remove("name of your session here");
gabsferreira
  • 3,089
  • 7
  • 37
  • 61
48

There is nothing like session container , so you can set it as null

but rather you can set individual session element as null or ""

like Session["userid"] = null;

Milan Mendpara
  • 3,091
  • 4
  • 40
  • 60
  • When I hit back it just loads up the main page even though the session is gone. – Si8 Dec 15 '15 at 21:24
32

you can use Session.Remove() method; Session.Remove

Session.Remove("yourSessionName");
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
5

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.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
2

HttpContext.Current.Session.Remove("sessionname");

it works for me

funbrain9
  • 503
  • 6
  • 15
1

A single way to remove sessions is setting it to null;

Session["your_session"] = null;
alvescleiton
  • 1,063
  • 11
  • 8