0

Q:

I know that to destroy all sessions , i can use:

Session.Abandon()

According to my question.

but why there is n't any equivalent method to destroy only one session. and i am supposed to use

Session.Remove("varName")

instead of.

Community
  • 1
  • 1
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

3 Answers3

7

First of all, you are talking about session variables, not sessions. Each user has one session and one Session object that you can store several variables in.

You use the Session.Abandon to abandon the session once the current request completes, which will remove the Session object.

You use the Session.Remove method to remove one variable from the Session object.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

Session.Clear() will remove all items from the session-state collection while Session.Remove(<item name>) will remove only the one item from the collection and leave the remainder intact. Session.Abandon() will destroy the whole session and start a new session.

Leons
  • 2,679
  • 1
  • 21
  • 25
1

You are mistaken between Session and Session Variables. A User viewing your site is assigned only 1 session and in this 1 session object, you can store many session variables.

Session.Abandon will remove the current session of the connected user.

Session.Remove will remove specific session variables as mentioned by you.

Session.Clear will remove all the session variables of the session but will not remove the session.


Refer to the following links for the Session and its usage:

CodeProject: Exploring Session in ASP.NET

ASP.NET Session State

ASP.NET Session state overview

Jayesh
  • 1,511
  • 1
  • 16
  • 37