Asp.net disables simultaneous login by the same user

Source: Internet
Author: User

There are several methods

First, set a Status field in the User table to go online to 0 and go offline to 1.

The second is to use global variables.

Application Cache

 

Here is the cache implementation for reference only

 

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 the string key = textbox1.text; // set the username text box to the cache keyword string uer = convert. tostring (Cache [Key]); // read the user's value in the cache // determine whether the user information in the cache exists. If no relevant value exists, it indicates that the user has not logged in.

If (uer = NULL | uer = string. empty) {// defines the cache expiration time timespan sesstimeout = new timespan (0, 0, system. web. httpcontext. current. session. timeout, 0, 0); // insert a user-related cache value at 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 {// re-log on to response. write ("") ;}< br/>

 

It is the same as the session expiration time.

 

 

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.