繁体   English   中英

在asp.net Web应用程序中查找有多少用户在线

[英]Find out how many users are online in asp.net web application

我正在开发一个Web应用程序,学生可以在其中进行考试。 这是显示在管理员和教员面板中登录哪些学生的要求。 首先,我更新了数据库中名为flag的列,该列在登录时变为1,在注销时变为0。但是当浏览器强行关闭时,无法更新。 为此,我使用了Java脚本方法,该方法会在我们导航到另一页面时触发,并显示学生已注销。

有没有相同的解决方案。

没有关闭用户浏览器而不注销的更新用户状态的简便方法。 您可以在Global.asax中使用Session_End事件,但这将在会话期满(通常为20分钟)时触发。 另外,在Session_End中,您没有有关已认证用户的信息,只有会话ID。

如果您使用ASP.NET成员身份对用户进行身份验证,则有一种方法可以满足您的需求

Membership.GetNumberOfUsersOnline()

将此代码添加到您的Global.asax页面上

public void Session_Start(object sender, EventArgs e)
{ 
 // Fires when the session is started 
 Application["UserCount"] = Convert.ToInt32(Application["UserCount"].ToString()) + 1;
}

public void Session_End(object sender, EventArgs e)
{ 
  // Fires when the session ends
  Application["UserCount"] = Convert.ToInt32(Application["UserCount"].ToString()) - 1;
}

将此代码添加到您的母版页上

private void Page_Load(System.Object sender, System.EventArgs e)
{ 
 //Put user code to initialize the page here
 this.lblUserCount.Text = "Users online " + Application["UserCount"].ToString();
}

要么

如果您使用内置的ASP.NET成员资格提供程序,则将有一个非常方便的Membership.GetNumberOfUsersOnline()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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