The same user name cannot be logged on simultaneously in Asp.net (Single Point login)
The common problem encountered in web projects is that the same user name is logged on multiple times, and there are many corresponding solutions. To sum up, these solutions: Put the user name after login into the database table; put the user name after login into the session; the user name after login into the application; the user name after login into the cache. Generally, after logging on, if you do not exit normally, the second login will not be allowed. In this case, there is usually a problem: if the user does not exit the system normally, he will be refused to continue logging on to the system due to issues such as the session has not expired, you can only log on after the session expires. The method described in this article is similar to the MSN login method. The First Login will be canceled during the second login, and the First Login will be similar to the MSN pop-up: Your account has been logged on elsewhere, prompt message about force deprecation.
Functions are also relatively simple to implement:
Enter the following code after the login user name and password are verified:
Hashtable honline = (hashtable) application ["online"];
If (honline! = NULL)
{
Idictionaryenumerator ide = honline. getenumerator ();
String strkey = "";
While (IDE. movenext ())
{
If (IDE. value! = NULL & ide. value. tostring (). Equals (userid ))
{
// Already Login
Strkey = ide. Key. tostring ();
Honline [strkey] = "xxxxxx ";
Break;
}
}
}
Else
{
Honline = new hashtable ();
}
Honline [session. sessionid] = userid;
Application. Lock ();
Application ["online"] = honline;
Application. Unlock ();
When you log on, place the login user name in a global variable online. Online is in the hashtable structure, key is sessionid, and value is the user name. During each login, the user determines whether the user name to be logged on already exists in online. If the user name already exists, the user name corresponding to the sessionid logged on to the first user is forcibly changed to xxxxxx, indicates that the login will be forcibly canceled.
Create a commonpage. All pages in the system are inherited from the commonpage. Add the following code to the background code of the commonpage:
Override protected void oninit (eventargs E)
{
Hashtable honline = (hashtable) application ["online"];
If (honline! = NULL)
{
Idictionaryenumerator ide = honline. getenumerator ();
While (IDE. movenext ())
{
If (IDE. Key! = NULL & ide. Key. tostring (). Equals (session. sessionid ))
{
// Already Login
If (IDE. value! = NULL & "xxxxxx". Equals (IDE. value. tostring ()))
{
Honline. Remove (session. sessionid );
Application. Lock ();
Application ["online"] = honline;
Application. Unlock ();
MessageBox ("your account has been logged in elsewhere, and you are forced to go offline! ", Login. aspx );
Return false;
}
Break;
}
}
}
}
When refreshing pages that inherit from commonpage, you must execute the code in the overloaded oninit to retrieve online, find the user's sessionid, and determine whether the user name in the sessionid has changed. If yes, then force the server to go offline, clear the session, and go to the login screen.
Finally, you need to release resources when the session expires or the system exits. Add the following code to session_end in the global. asax file:
Hashtable honline = (hashtable) application ["online"];
If (honline [session. sessionid]! = NULL)
{
Honline. Remove (session. sessionid );
Application. Lock ();
Application ["online"] = honline;
Application. Unlock ();
}
If the user does not normally log out and then re-log in, because of the high priority of the re-login, the user login will not be affected, and the resources occupied by the user who does not normally log out will be automatically cleared after the session expires, does not affect the system performance.
//////////////////////////////////////// //////////////////////////////////////// /////
Use Cache
String skey = username. Text. tostring (). Trim (); // obtain the value of the given key in the cache.
String suser = convert. tostring (Cache [skey]); // check for existence
If (suser = NULL | suser = string. Empty)
{
Timespan sesstimeout = new timespan (0, 0, system. Web. httpcontext. Current. session. Timeout, 0, 0); // get the session expiration time
Httpcontext. current. cache. insert (skey, skey, null, datetime. maxvalue, sesstimeout, system. web. caching. cacheitempriority. notremovable, null); // put the value into the cache to facilitate single-point Login
// Login successful
}
Else if (Cache [skey]. tostring () = skey) // if this account has been logged on
{
Clientscript. registerstartupscript (GetType (), "prompt", "<SCRIPT> alert ('Sorry, the current user has logged on '); </SCRIPT> ");
Return;
}
Else
{
Session. Abandon (); // This section is mainly used to prevent unnecessary errors and cause logon failure.
}