简体   繁体   English

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

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

i am developing a web application in which students give their exams. 我正在开发一个Web应用程序,学生可以在其中进行考试。 Here is requirement to show which students are logged in in admin and faculty panel. 这是显示在管理员和教员面板中登录哪些学生的要求。 First i have update a column in database named flag which turns to 1 on logged in time and on logged out turns to 0. But when browser forcefully closed it can not be updated. 首先,我更新了数据库中名为flag的列,该列在登录时变为1,在注销时变为0。但是当浏览器强行关闭时,无法更新。 for this, i have used java script method, which fires whenever we navigate to another page and shows the student logged out. 为此,我使用了Java脚本方法,该方法会在我们导航到另一页面时触发,并显示学生已注销。

Is there any solution for the same. 有没有相同的解决方案。

There is no easy way to update your user's status as soon as he closed the browser without logging out. 没有关闭用户浏览器而不注销的更新用户状态的简便方法。 You could use the Session_End event in Global.asax , but that will be triggered when the session expires (usually 20 minutes). 您可以在Global.asax中使用Session_End事件,但这将在会话期满(通常为20分钟)时触发。 Also, in Session_End you have no information about the authenticated user, only the session id. 另外,在Session_End中,您没有有关已认证用户的信息,只有会话ID。

If you are using ASP.NET Membership to authenticate your users, there is a method that does what you need 如果您使用ASP.NET成员身份对用户进行身份验证,则有一种方法可以满足您的需求

Membership.GetNumberOfUsersOnline()

Add this code on your Global.asax Page 将此代码添加到您的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;
}

Add this code on your Master Page 将此代码添加到您的母版页上

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

Or 要么

If you use the built-in ASP.NET membership provider, then there's the ever-so-handy Membership.GetNumberOfUsersOnline() method. 如果您使用内置的ASP.NET成员资格提供程序,则将有一个非常方便的Membership.GetNumberOfUsersOnline()方法。

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

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