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.
void button1_click (senderSystem. ) EventArgs e
string struser = string . Empty
This . TextBox1 . Text
Convert . ToString (Cache[strcachekey]
if (string. Empty
New TimeSpan (0,0,System. ) Web . HttpContext . Current . Session . Timeout ,0,0
-
cache insert ( strcachekey strcachekey ,null, datetime maxvalue sesstimeout cacheitempriority notremovable ,null);
Sessionstrcachekey
This . Label1 . Session ["User"]. ToString
This . Label1 . "What is this??????? "
Cache uses the cache to prevent duplicate logons to the same account