简体   繁体   中英

Online Users on ASP.NET Application

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.

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"];

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:

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM