Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Online active users counter in ASP.NET

0.00/5 (No votes)
30 Sep 2008 1  
With this tool which is written for ASP.NET, it is possible to count the number of online users, members, and guest users in websites.

Introduction

With this tool which is written for ASP.NET, it is possible to count the number of online users, members, and guest users in websites.

The tool installation is very simple and only takes a few minutes.

Step one - Add Reference

After downloading the attachment, you should add a reference to the project. If you know how to add references, please skip this step.

To add the reference, right click on your solution and select "Add Reference" from the menu. Then select the "OnlineActiveUsers.dll" file. Now the reference is added to the solution.

Step two - Change configuration

Open the "Web.config" file from your project (if this file is absent, right click on your project and from "Add new item", select "Web configuration file"). Then, add this code to the "web.config" file.

<httpModules>
  <add name="OnlineActiveUsers" 
       type="OnlineActiveUsers.OnlineUsersModule"/>
</httpModules>

Note that this code should be placed between the "system.web" tags.

Step three - Add some code to global.asax

Open the "global.asax" file in your project (if this file is absent, right click on your project and from "Add new item", select "Global Application Class"). In the "session_end" event, add this code:

OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.UpdateForUserLeave()

If the project is C#, the result should be like this:

void Session_End(object sender, EventArgs e)
{
    OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.UpdateForUserLeave();
}

Now the tool will start to count user statistics.

Step four - Access the statistics

To get the statistics, refer to the "OnlineActiveUsers.OnlineUsersInstance.OnlineUsers" variable.

Here are some of its important properties:

  • UsersCount: the number of all online users.
  • GuestUsersCount: the number of online guest users. This property works only if you have used the SetUserOffline and SetUserOnline methods. These methods are explained below.
  • RegistredUsersCount: the number of online members. This property works only if you have used the SetUserOffline and SetUserOnline methods. These methods are explained below.

Guest users and members

It is necessary to write some code to count the guest users and members correctly. Here is the explanation:

The first state we consider is when a user has entered your site and is logging into the site. To implement the process of counting guest users and members, there are several ways, but here I am using a simple way with the standard Login control in ASP.NET. If you are not using this control, skip this paragraph.

Add this code in the LoggedIn event of the Login control:

OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOnline(Login1.UserName)

For example, in C#, the code should be like this:

protected void Login1_LoggedIn(object sender, EventArgs e)
{
    OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOnline(Login1.UserName);
}

With this code, we specify that the user is logged in and will be counted as a member.

In a general way, when you're not using the Login control, the only thing that you should do is call this method after authenticating the user.

OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOnline(UserName)

The "UserName" parameter should be an authenticated user name.

The second state we use is when the user is already logged in and he is going to logout of his account. If you are using the "LoginStatus" control, add this code to the "LoggedOut" event:

OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOffline(User.Identity.Name)

For example, in C#, the code should be like this:

protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
{
    OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOffline(User.Identity.Name);
}

Here, "User.Identity.Name" is the logged in user name.

In a general way, use this method to count the user as a guest:

OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOffline(UserName)

In this code, the "UserName" parameter should be a user name. This name is case sensitive.

After these processes, the "GuestUsersCount" and "RegistredUsersCount" properties will show the number of guest users and members correctly.

Other methods

  • IsOnline method
  • This method returns a user's online state. The input parameter is the user name which is case sensitive, and the result is true if the user is online; otherwise, the result is false.

  • GetLastActivity method
  • This method returns the user's last activity time. The input parameter is the user name which is case sensitive, and the result is a "DateTime" if the user is online; otherwise, the result is "null" in C# and "Nothing" in VB.NET.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here