Signlog login implementation

Source: Internet
Author: User

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;

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.