Click here to Skip to main content
Click here to Skip to main content

Online active users counter in ASP.NET

, 30 Sep 2008
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SalarSoft
Web Developer
Anonymous Proxy Anonymous Proxy
No Biography provided

Comments and Discussions

 
Questionmy vote Pinmemberamnk.info2-Oct-13 2:20 
SuggestionVery easier built-in API! PinmemberJavid H17-Mar-13 19:43 
QuestionDoes not work well with ajax supported site PinmemberJonas T8-Jul-12 20:02 
GeneralMy vote of 1 PinmemberJonas T8-Jul-12 19:32 
The user count increases for every ajax request too.
GeneralGood Work. PinmemberNess20994-Jun-12 12:06 
QuestionMedium Trust PinmemberEric Beck17-May-12 4:32 
Questionuseronline PinmemberMember 458650617-Apr-12 22:20 
Questionabout IIS7 PinmemberUma Shankar Patel10-Nov-11 23:46 
QuestionAbout Dll that contains properties to count usres. PinmemberUma Shankar Patel24-Jul-11 2:54 
QuestionHow to count all Page Request? PinmemberDoan Quynh22-Feb-11 16:40 
GeneralMy vote of 1 Pinmembercrouchie199816-Oct-10 12:58 
QuestionSession handling PinmemberAutomation_lab9-Jul-10 1:29 
QuestionGuestUsersCount remains Zero PinmemberAfflatus21-Feb-10 5:15 
AnswerRe: GuestUsersCount remains Zero Pinmemberabartoch4-Nov-12 23:40 
QuestionIsOnline method Pinmemberkadherb14-May-09 20:32 
Generali enabeled it but got security exc Pinmemberskypainter1020-Jan-09 23:23 
GeneralRe: i enabeled it but got security exc PinmemberSalarSoft22-Jan-09 19:50 
Generaloops not working Pinmemberskypainter1018-Jan-09 22:21 
GeneralRe: oops not working PinmemberSalarSoft20-Jan-09 3:28 
Generalgreat man i like you,(if this dll will continue to work on production lol .thanks any way) Pinmemberskypainter1018-Jan-09 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 30 Sep 2008
Article Copyright 2008 by SalarSoft
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid