简体   繁体   中英

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

i am developing a web application in which students give their exams. 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. for this, i have used java script method, which fires whenever we navigate to another page and shows the student logged out.

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). Also, in Session_End you have no information about the authenticated user, only the session id.

If you are using ASP.NET Membership to authenticate your users, there is a method that does what you need

Membership.GetNumberOfUsersOnline()

Add this code on your Global.asax Page

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.

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