Our ASP. the net tutorial has repeatedly talked about the use of cache in ASP. NET 2.0, to prevent users from logging on to the same user at the same time, the home page should record the information of online users (Here we use the user name as an example ), 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.
Code As follows:
Put it in the place where the login is successful:
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 ");
}< br> else
{< br> // login again
response. write (" ");
}