The same user name cannot be logged on simultaneously in Asp.net (Single Point login)

Source: Internet
Author: User
Web
The problem frequently encountered in the project 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; use after login
Put the account name in the session; put the user name after login into the application; put the user name after login into the cache. Generally, after logging in
Frequent logout. The second login will not be allowed. In this case, there will usually be a problem: if the user does not normally exit the system, then he will continue to log on to the system due to issues such as the session has not expired,
Will be denied to continue logging on to the system. You can only log on after the session expires. The method described in this article is similar to the MSN login method. During the second login, the First Login will be canceled.
The next login will be similar to the MSN pop-up message: Your account has been logged in elsewhere, and you are forced to go offline.

Functions are also relatively simple to implement:

After the login user name and password are verified, enter the followingCode:

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. Each
During the next user login, determine whether the user name to be logged in already exists in online. If the user name already exists, set the user name corresponding to the sessionid logged in by the first user to strong
Change to xxxxxx, indicating 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.

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.