How to count sessions in asp.net server application

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

Leave a Comment