asp.net 2.0 prevents same user from landing at the same time

Source: Internet
Author: User
Tags httpcontext insert
asp.net

To prevent the same user from landing at the same time, the home page should record the information of the online user (here with the user name for example), and then determine whether the user is logged in. Here you use a cache to store the user name you have logged in. But there is also the question of knowing when users are leaving the system. This is to periodically clear the contents of the cache, that is, set a cache time. This time can be associated with the user's session value. When the user session value is invalidated, the user's information in the cache is also emptied. This is achieved to prevent the simultaneous landing effect, the specific code is as follows:

Put it where the landing was successful.

                         string key = textbox1.text;  //user name text box set to cache keyword
                          string uer = Convert.ToString (Cache[key]); //Read the user's corresponding value in Cache
                         //To determine if there is a user's information in cache, if there is no relevant value, the user did not login
                         if (Uer = null | | uer = = String.Empty)
                         {

Define cache Expiration Time

TimeSpan sesstimeout = new TimeSpan (0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);

Insert a user-related cache value on the first landing
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
{
Repeat Login
Response.Write (' Your account has landed! ') ("<script>alert"); window.location= ' login.aspx ';</script> ");
}



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.