This refers to the single point, generally refers to the Web service side, an account at the same time can only exist one ticket.
You may have encountered a problem in development, how to make the same user, at the same time only allowed to log in once. A lot of people would think of using an identity field in a database, log in to set 1, exit 0, log in to judge this field, if it is 1 to explain this user online, not allowed to login, this scheme seems to be effective, but in actual use found a lot of problems, for example, the user is not through the program in the Exit button exit, Instead of directly shutting down IE, the next time you log in to the user in the database online, the user will not be able to log in, of course, there are some ways to solve this problem: Add a timed job, regularly reset those users for a long time on-line, which will cause some problems,
If this user is actually using it for so long, it is manslaughter. Through many experiments, it is found that. NET itself can provide this solution.
The steps are as follows: first: Establish the Global.asax file, write the following code in the Session_End event: Hashtable h = (Hashtable) application["Online"];
if (H[session.sessionid]!= null) h.remove (Session.SessionID);
application["Online"] = h;
Second: Modify Web.config file, add <sessionstate mode= "InProc" in system.web node ></sessionState>
This is to use the Session_End event in Global.asax to take effect. Third: The page login event, to determine whether the logged-in user in the server-side global variables exist, there is not allowed to log on, do not exist to create.
Here is the implementation process, which is invoked in the event of the login button.
private void IsLogin () {Hashtable h = (Hashtable) application["Online"];
if (h = = null) {h = new Hashtable (); //Verify that the user exists in the application (online) idictioNaryenumerator e1 = H.getenumerator ();
BOOL flag = FALSE; while (E1. MoveNext ()) {if (Checkcookie) (E1.
Value.tostring ()) {flag = true;
Break } if (flag) {Response.Write ("<script defer language= ' JavaScript ' >alert (' T His user is online! ');
History.go ( -1);</script> "); else {loginlogic login = new Loginlogic (this.txt_user_id. Text.trim (), This.txt_password.
Text.trim ()); if (!login.getloginstatus) {Response.Write ("<script defer language= ' JavaScript ') >alert (' Invalid UserID or password. Please try again. ');
</script> ");
else {//Generate server-side identity value DateTime now = DateTime.Now; String cookievalue = Now. Year.tostring () + now. Month.tostring () + now. Day.tostring () + now. HOur. ToString () + now. Minute.tostring () + now.
Second.tostring () + now.Millisecond.ToString (); Writes the UserID + identity value to the global variable table H[session.sessionid] = this.txt_user_id.
Text.trim () + "-" + cookievalue;
application["Online"] = h; Writes the identity value to the client cookie response.cookies["HQs"].
Value =cookievalue; response.cookies["HQs"].
Expires = DateTime.Now.AddDays (1); User information is recorded in the session session["userid"] = this.txt_user_id.
Text.trim ();
Response.Redirect ("manage/index.aspx");
}} private bool Checkcookie (string appvalue) {bool isexist = false; if (request.cookies["HQs"]!= null) {string cookievalue = request.cookies[' HQs '].
Value;
char[] sp = new char[1]{'-'}; String Appuserid = Appvalue.split (sp) [0].
ToString (); String Appcookie = Appvalue.split (sp) [1]. ToStRing (); if (Appuserid = = this.txt_user_id.
Text.trim () && appcookie!= cookievalue) isexist = true;
return isexist;
Note: Testing may be problematic in VS2005 's built-in Web server, and is still being tested in the formal environment of IIS. Description of this scenario: In general, the session timeout time is 20 minutes, that is, if the user directly shut down the browser, and then login again, this time session has not expired, all will not trigger the Global.asax in the Session_ End event, the user is prompted to be online, after 20 minutes after the event is executed, the inactive user will be deleted, and then log on normally.
So do not think that the direct switch off ie, and then login can not go, that the scheme is invalid. Of course, the session timeout time can be modified, 20 minutes is not suitable to change. The change is as follows: <sessionstate mode= "InProc" timeout= "You think the appropriate value" ></sessionState>------------------------continued, Now there's a new problem, if the user quits normally, the session will not execute the end event in 20 minutes, which means the user is still online, so the user's login again won't be allowed. This is obviously a bit unreasonable.
This plan is not perfect enough, I hope you can add. Add: The third step of the code has been updated to solve this problem two, for some reason, in our application will encounter a user can only be logged in one place, that is, we usually call the single sign-on.
In the asp.net to achieve a single sign-on is very simple, the following is the main method and all the code analysis. Implementation of the idea of using the cache function, we put the user's login information in the cache, and set the expiration time for the session expiration time, so, once the session expires, our cache is also expired, and cache for all users can access, therefore,
It is easier to save user information with it than the database. Program code String SKey = Username. Text.tostring (). Trim (); Get the given k in cacheEY value String suser = Convert.ToString (Cache[skey]); Check for presence if (Suser = null | | suser = = String.Empty) {times
Pan sesstimeout = new TimeSpan (0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);//Get the expiration time of session HttpContext.Current.Cache.Insert (SKey, SKey, NULL, DateTime.MaxValue, Sesstimeout,
System.Web.Caching.CacheItemPriority.NotRemovable, NULL)//Put the value into the cache's own single sign-on//login successfully} else if (Cache[skey). ToString () = = SKey)//If this account is already logged in {Clientscript.registerstartupscript (GetType), "hint", "<s
Cript>alert (' Sorry, current user has logged in ');</script> ");
Return
else {Session.Abandon ();//This paragraph is mainly to avoid unnecessary errors caused by the failure to log in}