Online Users on ASP.NET Application
Asked Answered
N

5

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.

Nagy answered 13/10, 2015 at 1:25 Comment(3)
This suggests SimpleMembership does not implement that function.Architectonics
You want to know the number of users logged in? or the number of users total online?Yuonneyup
@SimchaKhabinsky Total number of users onlineNagy
L
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.

Lunalunacy answered 20/10, 2015 at 17:17 Comment(0)
A
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"];
Allay answered 15/10, 2015 at 14:51 Comment(2)
This suggests that if you add something to the session then it will fire. Have you tried adding something to session in session start? https://mcmap.net/q/544113/-session_end-does-not-fireAllay
Also please note, The Session_End event is raised only when the sessionstate mode is set to InProcSpartacus
P
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

Pigtail answered 21/10, 2015 at 10:6 Comment(0)
S
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:

Seger answered 16/10, 2015 at 15:26 Comment(1)
This, unfortunately, is not available in the newer ASP.Net Identity model. :(Willing
I
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

Indefatigable answered 20/10, 2015 at 15:14 Comment(0)
L
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.

Lunalunacy answered 20/10, 2015 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.