10

Saw so many articles and links related to the same concept.

Counting online users using asp.net

Is there any ASP.NET application to monitor online user and page view log report?

Mine is little different. My application is MVC4 and using SimpleMembershipProvider. I am not sure why GetNumberOfUsersOnline is not working.

https://msdn.microsoft.com/en-us/library/system.web.security.membership.getnumberofusersonline(v=vs.110).aspx

Any Ideas ? How to do this in a easy and efficient way. I am just using this in only one place in my website.

Community
  • 1
  • 1
Chatra
  • 2,989
  • 7
  • 40
  • 73
  • 2
    [This](http://forums.asp.net/t/1847047.aspx?SimpleMembershipProvider+GetNumberOfUsersOnline+Method+MembershipUser+LastLoginDate+Property) suggests SimpleMembership does not implement that function. – Jasen Oct 15 '15 at 02:01
  • You want to know the number of users logged in? or the number of users total online? – Simcha Khabinsky Oct 21 '15 at 16:32
  • @SimchaKhabinsky Total number of users online – Chatra Oct 23 '15 at 14:32

5 Answers5

5

I found this online it it looks like it will work for you. Just add this code to your Global.asax.cs file:

protected void Application_Start(Object sender, EventArgs e)
{

    Application.Lock();
    Application["UserCount"] = 0;

}
protected void Session_Start(Object sender, EventArgs e)
{

    Application.Lock();
    Application["UserCount"] = (int)Application["UserCount"] + 1;
    Application.UnLock();
}

protected void Session_End(Object sender, EventArgs e)
{
    Application.Lock();
    Application["UserCount"] = (int)Application["UserCount"] - 1;
    Application.UnLock();

}

Then when you need to access the user count you can use:

var count = (int)Application["UserCount"];
Aaron
  • 1,361
  • 1
  • 13
  • 30
  • This suggests that if you add something to the session then it will fire. Have you tried adding something to session in session start? http://stackoverflow.com/questions/4813462/session-end-does-not-fire – Aaron Oct 15 '15 at 15:11
  • Also please note, The `Session_End` event is raised only when the `sessionstate` mode is set to `InProc` – KyleMit Aug 31 '16 at 15:49
3

You can use signalR to track connected users. Using this you can get count online efficiently & Real Time and also track connected users information. You can put condition to track logged in users also. So, Go with latest technology. You can implement with existing MVC application.

You can refer this official tutorial for the same.

http://www.asp.net/signalr

2

From Microsoft documentation:

GetNumberOfUsersOnline returns the number of users for the current ApplicationName where the last-activity date is greater than the current time less the UserIsOnlineTimeWindow. The last-activity date/time stamp is updated to the current date and time when user credentials are validated by way of the ValidateUser or UpdateUser method or when a call to a GetUser overload that takes no parameters or one that uses the userIsOnline parameter to specify that the date/time stamp should be updated.

You can see that GetNumberOfUsersOnline depends on multiple parameters and isn't efective. As a workaround I suggest that you could nherits SqlMembershipProvider and override the GetNumberOfUsersOnline(), so you cam implement your logic here.

public class MyMembershipProvider : SqlMembershipProvider
{
  public override bool ValidateUser(string username, string password)
  {
    if (base.ValidateUser(username, password))
    {
        // successfully logged in. add logic to increment online user count.

        return true;
    }

    return false;
 }

 public override int GetNumberOfUsersOnline()
 {
    // add logic to get online user count and return it.
 }
}

Just decrement online user count logic in user log out

If you want track visitors and pages visited, here some idea:

Donald
  • 534
  • 1
  • 10
  • 18
2

You may try to read the performance counters listed here :

Current Anonymous Users is the number of anonymous IIS users

Current Non-Anonymous Users is the number of authorized IIS users

Community
  • 1
  • 1
Dexion
  • 1,101
  • 8
  • 14
2

Another nice solution that is pleasantly orthogonal to your code is Google Analytics. http://www.google.com/analytics/ Using GA, you can see a real-time display of active users on your website. It also helps that you have a history over time and can see peaks and valleys of user activity.

Todd Sprang
  • 2,899
  • 2
  • 23
  • 40