Glossary
1. Cookies are translated as "dessert" by English words. This seems to have nothing to do with our computer. Cookies refer to a piece of information stored on a computer on a server.
2. The Session is translated as "Session" by English words. It exists in the memory of the server.
Scenarios where user logon is restricted
1. For example, after we log on to QQ in one place. If we log in again on another computer, the system will be kicked off the server.
2. The mailbox I used also has the above situation.
This is guaranteed to a great extent. Account Security.
How to Implement
Method 1: when a user logs on, the user's logon status is recorded in the database. Each time we log on to the database, we find whether the user is logged on. If you have logged on, perform relevant business operations.
Method 2: Put the user's logon status in the Session
Method 1 obviously has a disadvantage, assuming that the server is suddenly powered off. Is it a tragedy. The status of the logged-on user will be messy, and the user will not be able to log on again. Therefore, we choose Cookies and sessions to implement this scenario.
Sample Code
Copy codeThe Code is as follows:
If (Session ["LOGIN_USER_LIST"]! = Null)
{
Dictionary <Guid, UserInfo> Userlist = Session ["LOGIN_USER_LIST"] as Dictionary <Guid, UserInfo>;
Guid LoginKey = new Guid ();
Guid. TryParse (CookieHelper. getCookie ("LOG_USER_KEY"), out LoginKey );
If (Userlist. Keys. Contains (LoginKey ))
{
// How to log on
}
Else
{
// If the user does not log on
}
}
I hope the simple code above will help you.