Cache prevents duplicate logins for the same account using cache

Source: Internet
Author: User

Requirements Overview
For b/S application system customers will often put forward the same account can not repeat the need to log in, that is, with an account login system, in the case of the system does not time out, no one can use the currently logged account login system. This requirement is also included in my current project.
In fact, to achieve this function is not difficult, there are many methods, such as using a database to record user login, with application to save user login information, using the cache to save information and so on. Now let's talk about how you can easily implement this feature with cache caches.


Workaround
We all know that there is a difference between the two states of the cache and the session, the cache is a global object that is scoped to the entire application, all users, and the session is a user sessions object, which is a local object that holds the information for a single user.


We simply store the user information after each user login in the cache, the key name of the cache is the user's login name, the cache expiration time is set to the session time-out, the user every time you log in to determine the cache[user name] whether there is a value, if there is no value, proves that the user is not logged on, otherwise the user is logged in. For specific implementations, see the following example:

Hide Line number copy code ? This is a program code.
  1. void button1_click (senderSystem. ) EventArgs e
  2.          
  3.                 string struser = string . Empty
  4.                  This . TextBox1 . Text
  5.                 Convert . ToString (Cache[strcachekey]
  6.                 if (string. Empty
  7.                 
  8.                     New TimeSpan (0,0,System. ) Web . HttpContext .  Current . Session . Timeout ,0,0
  9.   cache   insert   ( strcachekey   strcachekey  ,null,  datetime   maxvalue   sesstimeout   cacheitempriority   notremovable  ,null); 
  10.                     Sessionstrcachekey
  11.                      This . Label1 .   Session ["User"].  ToString
  12.                 
  13.                 
  14.                 
  15.                      This . Label1 . "What is this??????? "  
  16.                 
  17.          

Cache uses the cache to prevent duplicate logons to the same account

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.