Assuming you want to do this in your ASP.Net code, and not by using a web server tool, you can increment a counter in an Application (or Cache) variable on Session_Start
, and decrement it on Session_End
in Global.asax
.
If you want to know more than the count of active users, you can accumulate user information in a collection there -- a List<T>
of User objects, perhaps.
Here's some code to get you started with this approach:
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["SessionCount"] = Convert.ToInt32(Application["SessionCount"]) + 1;
Application.UnLock();
}
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
Application["SessionCount"] = Convert.ToInt32(Application["SessionCount"]) - 1;
Application.UnLock();
}