ASP. NET 2.0 prevents simultaneous login by the same user

Source: Internet
Author: User

From: http://www.cnblogs.com/ly5201314/archive/2008/09/04/1284149.html

 

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.CodeAs 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]); // reads the user's value in the cache
// determines whether the cache contains user information. If no relevant value exists, the user has not logged on
If (uer = NULL | uer = string. empty)
{< br> // defines the cache expiration time
timespan se Sstimeout = 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 (" ");
}

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.