There is a way to manage how many sessions an asp.net running aplication have? I want to exhibit it in a page, maybe with some other important information, if available. And, how can I do it?
Asked
Active
Viewed 1.7k times
7
-
Yes, it is! But if we can obtain more valuable information, like resources consuption, it should be great! – Alex Jun 02 '11 at 18:28
-
Ok. In my answer, you could retrieve, update, and store an array (instead of individual App elements) of user information from Session as well. You would have one nice array of all your user info. – Nick Rolando Jun 02 '11 at 19:29
-
is your App using ***stateserver*** or ***InProc*** ? Maybe using _Performance Monitor stats_ (programmatically via *WMI*) – Kiquenet Jun 23 '16 at 08:47
2 Answers
17
In global.asax
, do the following:
Handle the Application.Start
event adding the following:
Application["LiveSessionsCount"] = 0;
Handle the Session.Start
event adding the following:
Application["LiveSessionsCount"] = ((int) Application["LiveSessionsCount"]) + 1;
Handle the Session.End
event adding the following:
Application["LiveSessionsCount"] = ((int) Application["LiveSessionsCount"]) - 1;
To retrieve sessions count inside your page write the following:
int LiveSessionsCount = (int) Application["LiveSessionsCount"];

Akram Shahda
- 14,655
- 4
- 45
- 65
-
1@Haris Read: http://www.codeproject.com/Articles/21156/ASP-NET-HttpModule-for-handling-session-end-with-S – Akram Shahda Jun 23 '14 at 12:35
-
2see https://stackoverflow.com/a/41192294/903783 - lock/unlock is needed since read, increment and set a value are three steps and in between them sessions may have been created or terminated in parallel – George Birbilis Jan 07 '19 at 09:44
2
Perhaps in your global.asax file Session_Start and Session_End events, you can store session information to a userinfo array within your application state object. Then you can manage this array from App State throughout your application.

Nick Rolando
- 25,879
- 13
- 79
- 119
-
This solution counts all values stored in the application object, not just the number of sessions. It also wastes a large amount of memory for busy sites. All you need is a single integer. – ShadowChaser Jun 02 '11 at 18:24
-
1@ShadowChaser He wants to 'manage' Session information, not just have a count of how many people are logged in. Although, I should have a single array within App State that stores this information. – Nick Rolando Jun 02 '11 at 19:32
-
1Note: *stateserver* `Session_End` will not work , only for ***InProc*** – Kiquenet Jun 23 '16 at 07:53