WEB projects often encountered problems are the same user name multiple landing problems, the corresponding solution is also a lot, summed up is nothing more than these solutions: the login after the user name into the database table; login username placed in the session; Login to Application The login username is placed in the cache. In general, these methods are after landing, if not normal exit, the second landing will not be allowed. This generally has a problem: if the user does not normally exit the system, then he continued to login, because the session is not expired and other issues, will be refused to continue to log on the system, only wait until the session expires before landing. This article introduces the method is similar to the MSN Landing method, the second landing will be the first time to log off the landing, the first landing will be similar to MSN Pop-up: Your account has been landed elsewhere, you are forced to go offline prompt information.
Features are also relatively simple to implement:
Login Username password verification by entering the following code:
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 the user logs in, the user name is placed in a global variable online,online to hashtable structure, the key is sessionid,value for the user name. Each time the user logged in to determine whether the following user name in online is already exist, if there is a user name has been logged in, the first person logged in the SessionID corresponding user name is forced to xxxxxx, indicating that the login will be forced to log off.
Create a Commonpage page, all the pages in the system inherit from the Commonpage page, and add the following code to the Commonpage page's background code:
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 landed elsewhere, you are forced to go offline!") ", login.aspx);
return false;
}
Break
}
}
}
}
The page that inherits from the Commonpage executes the overloaded OnInit code in the refresh, takes out online, finds the corresponding SessionID of the user, determines whether the corresponding user name in the SessionID is changed, and if so, forces the downline to clear the session, Go to the login screen.
Finally, you need to release the resource when the session expires or exit the system, and add the following code to the 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 exit after the re-login, because the priority of heavy landing, will not affect the user's landing, and the abnormal exit of the user's resources will be automatically cleared after the session expires, will not affect the performance of the system.
Finally, you need to release the resource when the session expires or exit the system, and add the following code to the 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 exit after the re-login, because the priority of heavy landing, will not affect the user's landing, and the abnormal exit of the user's resources will be automatically cleared after the session expires, will not affect the performance of the system.
The same user name is logged in ASP. Log off the previous user (GO)