ASP.NET 应用程序的在线用户

Online Users on ASP.NET Application

看到很多与同一概念相关的文章和链接。

Counting online users using asp.net

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

我的有点不同。我的应用程序是 MVC4 并使用 SimpleMembershipProvider。我不确定为什么 GetNumberOfUsersOnline 不起作用。

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

有什么想法吗?如何以简单有效的方式做到这一点。我只是在我网站的一个地方使用它。

我在网上找到 this 它看起来对你有用。只需将此代码添加到您的 Global.asax.cs 文件中:

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();

}

然后当您需要访问用户数时,您可以使用:

var count = (int)Application["UserCount"];

来自微软文档:

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.

您可以看到GetNumberOfUsersOnline 依赖于多个参数并且是无效的。 作为解决方法,我建议您可以继承 SqlMembershipProvider 并覆盖 GetNumberOfUsersOnline(),这样您就可以在此处实现您的逻辑。

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.
 }
}

只是在用户注销时减少在线用户计数逻辑

如果您想跟踪访问者和访问过的页面,这里有一些想法:

您可以尝试阅读列出的性能计数器 here :

Current Anonymous Users is the number of anonymous IIS users

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

另一个与您的代码正交的不错的解决方案是 Google Analytics。 http://www.google.com/analytics/ 使用 GA,您可以在您的网站上看到活跃用户的实时显示。它还有助于您了解一段时间内的历史记录,并可以看到用户 activity.

的高峰和低谷

您可以使用 signalR 来跟踪已连接的用户。使用此功能,您可以有效地实时在线计数,还可以跟踪连接的用户信息。您也可以设置条件来跟踪登录用户。因此,采用最新技术。您可以使用现有的 MVC 应用程序来实现。

同样可以参考官方教程。

http://www.asp.net/signalr