To prevent the same user from logging on at the same time, the home page should record the information of the online user (Here we use the user name as an example), and then determine whether the Login User already exists. Here, we use a cache to store the user name that has been logged in. But the other question is, when do users leave the system? This requires that the content in the cache be cleared regularly, that is, set a cache time. This time can be associated with the user's session value. When the user's session value fails, the user's information in the cache will also be cleared. this achieves the effect of preventing simultaneous login. The specific code is as follows:
String key = TextBox1.Text; // set the username text box to the cache keyword.
String uer = Convert. ToString (Cache [key]); // read the value of the user in the cache
// Determine whether the cache contains user information. If no relevant value exists, it indicates that the user has not logged in.
If (uer = null | uer = String. Empty)
{
// Define the cache expiration time
TimeSpan SessTimeout = new TimeSpan (0, 0, System. Web. HttpContext. Current. Session. Timeout, 0, 0 );
// Insert a user-related cache value during the first login,
HttpContext. Current. Cache. Insert (key, key, null, DateTime. MaxValue, SessTimeout, System. Web. Caching. CacheItemPriority. NotRemovable, null );
Session ["ADMINID"] = TextBox1.Text;
Response. Redirect ("main. aspx ");
}
Else
{
// Repeated login
Response. Write ("<script> alert ('your account has logged on! '); Window. location = 'login. aspx'; </script> ");
}