Home > OS >  I want to know how Session.Clear () works on ASP.NET
I want to know how Session.Clear () works on ASP.NET

Time:12-27

Session.Clear (); Does this empty all session on asp.net? Can I delete a specific session, such as Session["USER_ID"].Remove()? Or session["USER_ID"].Clear ();

CodePudding user response:

Session.Clear() can be used if you want that the user remaining in the same session (prevent him from doing relogin) and just reset all of his session specific data. The reason for that is because Session.Clear() Removes all the keys and values from the session-state collection. It actually removes all values from the Object. The session with the same key is still alive.

By the way, the Session.Abandon() removes all the objects stored in a Session.

So, if you use Session.Abandon(), you lose that specific session and the user will get a new session key. You could use it for example when the user logs out.

Check out this article for more info - http://csharpexample.com/CsharpExampleSeeDetail.aspx?Did=78&CatID=29

CodePudding user response:

Session.Clear (); Removes all keys and values from the session-state collection.

Session.Clear (); remove values in session state

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.

  • Related