The same user name is logged in ASP. Log off the previous user (GO)

Source: Internet
Author: User

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)

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.