How to remove specific session in asp.net?

前端 未结 6 1815
醉酒成梦
醉酒成梦 2020-12-28 12:54

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

  1. Session[\"userid\"] = UserTbl.userid;
  2. Session[\"userType\"] = UserTbl.type;
相关标签:
6条回答
  • 2020-12-28 13:16

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

    Session.Remove("yourSessionName");
    
    0 讨论(0)
  • 2020-12-28 13:16

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

    Session["your_session"] = null;
    
    0 讨论(0)
  • 2020-12-28 13:24

    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;

    0 讨论(0)
  • 2020-12-28 13:28

    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.

    0 讨论(0)
  • 2020-12-28 13:30

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

    it works for me

    0 讨论(0)
  • 2020-12-28 13:31
    Session.Remove("name of your session here");
    
    0 讨论(0)
提交回复
热议问题