简体   繁体   中英

How to remove specific session in asp.net?

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?

Session.Remove("name of your session here");

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;

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

Session.Remove("yourSessionName");

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 .

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

it works for me

删除会话的一种方法是将其设置为 null;

Session["your_session"] = null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM