Question

Is there a way to determine the number of users that have active sessions in an ASP.NET application? I have an admin/tools page in a particular application, and I would like to display info regarding all open sessions, such as the number of sessions, and perhaps the requesting machines' addresses, or other credential information for each user.

Was it helpful?

Solution

ASP.NET Performance Counters like State Server Sessions Active (The number of active user sessions) should help you out. Then you can just read and display the performance counters from your admin page..

OTHER TIPS

In global.aspx

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Application["OnlineUsers"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
    // Code that runs when a new session is started
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
    Application.UnLock();
}

void Session_End(object sender, EventArgs e)
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate 
    // mode is set to InProc in the Web.config file. 
    // If session mode is set to StateServer or SQLServer, 
    // the event is not raised.
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
    Application.UnLock();
}

Note: The Application.Lock and Application.Unlock methods are used to prevent multiple threads from changing this variable at the same time.

In Web.config

Verify that the SessionState is "InProc" for this to work

    <system.web>
        <sessionState mode="InProc" cookieless="false" timeout="20" />
    </system.web>

In your .aspx file

Visitors online: <%= Application["OnlineUsers"].ToString() %>

Note: Code was originally copied from http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx (link no longer active)

If you are using .net Membership you could use

Membership.GetNumberOfUsersOnline()

More about it: http://msdn.microsoft.com/en-us/library/system.web.security.membership.getnumberofusersonline.aspx

If you'd like to implement the same mechanism by yourself, you can define like a CurrentUserManager class and implement the singleton pattern here. This singleton object of class CurrentUserManager would be unique in the AppDomain. In this class you will create its self instance once, and you will prohibit the others from creating new instances of this class by hiding its constructor. Whenever a request comes to this object, that single instance will give the response. So, if you implement a list that keeps the records of every user (when a user comes in, you add him to the list; when he goes out, you remove him from the list). And lastly, if you want the current user count you could just ask the list count to this singleton object.

The way I've seen this done in the past is adding extra code to the Session_OnStart event in the Global.asax file to store the information in a session agnostic way, e.g. a database or the HttpApplicationState object. Depending upon your needs you could also use Session_OnEnd to remove this information.

You may want to initialise and clean up some of this information using the Application_Start and Application_End events.

The administration page can then read this information and display statistics etc.

This is explained in more depth at http://msdn.microsoft.com/en-us/library/ms178594.aspx and http://msdn.microsoft.com/en-us/library/ms178581.aspx.

if you use sql server as the session state provider you can use this code to count the number of online users:

SELECT Count(*) As Onlines FROM ASPStateTempSessions WHERE Expires>getutcdate()

You can use PerformanceCounter to get data from System.Diagnostics namespace. It allows you to get "Sessions Active" and much more. It allows you to get from local server as well as remote.

Here is an example of how to do it on local machine

void Main()
{
    var pc = new PerformanceCounter("ASP.NET Applications", "Sessions Active", "__Total__");

    Console.WriteLine(pc.NextValue());
}

or for remote server you would do:

void Main()
{
    var pc = new PerformanceCounter("ASP.NET Applications", "Sessions Active", "__Total__", "ServerHostName.domain");

    Console.WriteLine(pc.NextValue());
}

Performance Counters for ASP.NET provides full list of ASP.NET counters that you can monitor

Google Analytics comes with an API that can be implemented on your ASP.NET MVC Application. It has RealTime functionality so the current amount of users on your website can be tracked and returned to your application.

Here's some information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top