How to count sessions in asp.net server application

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?


Solution 1:

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"];

Solution 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.