6

I am using the ASP.NET framework( with C#) and I make Session variables for every user. Is it possible to iterate through all Sessions that have been intialized. In this iteration I would also like to change certain properties of some of the sessions.

I am trying to change some data that is stored in session variables. I have session Ids of the particular users for whom I want to change their sessions, so I would also need to be able to compare the Ids.

Borut Flis
  • 15,715
  • 30
  • 92
  • 119
  • 2
    Let's assume the answer is "Yes". What do you need next? Your question is incomplete. – George Stocker Nov 06 '12 at 21:07
  • 1
    Duplicate of either [question 1](http://stackoverflow.com/questions/1470334/list-all-active-asp-net-sessions) or [question 2](http://stackoverflow.com/questions/8854176/get-a-list-of-all-active-sessions-in-asp-net). – Candide Nov 06 '12 at 21:29
  • http://weblogs.asp.net/imranbaloch/archive/2010/04/05/reading-all-users-session.aspx – Tariqulazam Nov 06 '12 at 21:36
  • @GeorgeStocker The question is complete. Looking for a way to iterate through all sessions. What the user wants to do after this is irrelevant. – Paul Nov 13 '17 at 17:31

2 Answers2

0

I am not sure whether there are any framework classes for handling this, but if you really need to loop through the sessions created on the IIS server for each request, why not store each session in your own collection object that you can access from your code? For example, in the global.asax file, you can add your own code in the Session_Start event, to save the specific session to your List. You will have to check whether the session is a new session, which can be done via Session.IsNewSession property. Everytime a new session is created, the Session_Start event in Global.asax is fired.

But there can be issues if you don't remove sessions from your List when they timeout or end, so how I might go about doing this is:

  • In Session_Start event, Check for this.Session.IsNewSession boolean value

  • If Session.IsNewSession is true, get current session (using this.Session, because Global.asax has the current new session in its context), and save it in a Dictionary object with the key as Session.SessionId.

  • This will create a unique key-pair collection for each Session that is created in the server.

  • In Session_End event, get the Session.SessionID property of the current Session (this.Session), which is the one which has ended.

  • Use the Session.SessionID of the finished Session value to remove the key value pair in the Dictionary containing the Sessions.

Once this infrastructure is in place, and the Dictionary object resides in a place which can be accessed by your application code, you can retrieve this dictionary and iterate through it to get the Sessions active in the Server at that point in time.

  • This does not work. Code from a controller after login will show the `Session.SessionId` as 'foo' (an example). I loop my static list of sessions and find the same one with 'foo'. But the rest of the properties do not match. It's like there are two copies of it. – Paul Nov 13 '17 at 17:34
0

You can collect all the session keys as follows. then access the session value using this key.

 var AllSessionKeyCollection = Session.Keys;
  • This would seem to look at all values stored against the current session, not all sessions present across all users. – TZHX Jul 06 '22 at 09:05
  • Accessing all sessions present across all users open up security threat so I think it is not achievable, and probably restricted. – Jyotirmaya Das Jul 06 '22 at 13:17