About re-login!
1: Set in Global.
// Add Init and the AcquireRequestState event;
Public void Init (HttpApplication application)
{
Application. AcquireRequestState + = new EventHandler (application_AcquireRequestState );
}
// Implement AcquireRequestState, which is executed every time the client responds;
Private void application_AcquireRequestState (object sender, EventArgs e)
{
System. Web. HttpApplication App = (HttpApplication) sender );
If (App. Context. Session = null) return;
If (App. Context. Session ["userID"] = null) return;
System. Data. DataTable dt = (System. Data. DataTable) Application ["userTable"];
If (dt. Select ("userID =" + Session ["userID"]. ToString (). Length> 0)
{
Dt. Rows [0] ["loginTime"] = System. DateTime. Now;
Dt. AcceptChanges ();
}
}
// Timer Interval
Private int interval = 20;
// In Application_Start, 1: create an online global user table; 2: register a timer event (used to maintain the online user table at a certain interval ).
Protected void Application_Start (Object sender, EventArgs e)
{
// -- 1 application user table;
System. Data. DataTable dt = new DataTable ();
Dt. Columns. Add ("userID ");
Dt. Columns. Add ("loginTime ");
Dt. PrimaryKey = new System. Data. DataColumn [] {dt. Columns ["userID"]};
Dt. AcceptChanges ();
Application. Lock ();
Application ["userTable"] = dt;
Application. UnLock ();
// -- 2 Timer
System. Timers. Timer tm = new System. Timers. Timer ();
Tm. Interval = 60000 * this. interval;
Tm. Elapsed + = new System. Timers. ElapsedEventHandler (tm_Elapsed );
Tm. Start ();
}
// Timer event;
Private void tm_Elapsed (object sender, System. Timers. ElapsedEventArgs e)
{
DeleteTimeOutUser ();
}
// Delete expired online users;
Private void deleteTimeOutUser ()
{
If (Application ["userTable"] = null) return;
System. Data. DataTable dt = (System. Data. DataTable) Application ["userTable"];
Foreach (System. Data. DataRow dr in dt. Rows)
{
If (System. DateTime. Compare (System. DateTime) dr ["loginTime"]). AddMinutes (2), System. DateTime. Now) <0)
Dr. Delete ();
}
Dt. AcceptChanges ();
}
// ------------------------------ Process of clicking exit -----------------------------------------
// Exit
Public void reLogin (System. Web. UI. Page currentPage)
{
If (currentPage. Session! = Null) & (currentPage. Session ["userID"]! = Null ))
{
This. deleteUser (int. Parse (currentPage. Session ["userID"]. ToString (), currentPage. Application );
}
CurrentPage. Session. Abandon ();
}
// Delete the userID of the current user in the application;
Private void deleteUser (int userID, System. Web. HttpApplicationState Application)
{
If (Application ["userTable"] = null) return;
System. Data. DataTable dt = (System. Data. DataTable) Application ["userTable"];
Foreach (System. Data. DataRow dr in dt. Rows)
{
If (int. Parse (dr ["userID"]. ToString () = userID)
Dr. Delete ();
}
Dt. AcceptChanges ();
}
If the application uses the Form Authentication mode when deleting online users, the System. Web. Security. FormsAuthentication. Signout ()
----------------------------------------------------------------------------
Issues left over. Although the timeout cancellation problem is solved, how can users send an exit request to the server and delete the current user ID when they exit IE;