asp.net in the same user name can not be logged at the same time (single point of landing)

Source: Internet
Author: User
Tags exit log net tostring
asp.net

The problem that is frequently encountered in Web projects is the problem of landing the same user name multiple times. The corresponding solution is also a lot, summed up all these solutions: the user name after the landing into the database table, the user name after landing into the session, the user name after landing into the application The user name after landing is placed in the cache. Generally these methods are logged after, 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 log on, because the session did not expire and other issues, will be refused to continue landing system, can only wait for the session expired before landing. This article introduces the method is similar to the MSN Landing method, the second landing will be the first landing log off, the first landing will be similar to the MSN pop-up: Your account has been landed elsewhere, you are forced offline prompt information.
Features are also simple to implement:

Enter the following code after the login username password Authentication:

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 login user name is placed in a global variable online,online as the Hashtable structure, and the key is sessionid,value as the username. Every time the user login to determine whether the following user name in online is not already exist, if the existence of the user name has been landed, the first person landed sessionid corresponding user name mandatory change to XXXXXX, said that the landing will be forced off.

Create a Commonpage page where 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 get off the line!") ", login.aspx);
return false;
}
Break
}
}
}

}

Inherited from the commonpage of the page in the refresh to perform the overloaded OnInit code, take out online, find the user corresponding to the SessionID, to determine whether the corresponding user name SessionID change, if the change, forced offline, clear off the session, Go to the login screen.

Finally, you need to release resources when the session expires or when you exit the system, adding the following code in 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 normally exit after landing, because the priority of the landing will not affect the user's landing, and the user of the abnormal exit will automatically clear after the session expires, will not affect the performance of the system.




Related Article

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.