The function of this example is to prevent users from logging in! If the user has already logged in, then when it logs in again, return to the prompt box!
Implementation: After the user login is successful, store the user login information to the use of the HashTable type, whose key value is sessionid, its value value is the user ID; when the user cancels, call session.abandon; in Global In the sessionend event in .asax, delete the user ID from the HashTable; when the user accesses the page, check if there is a corresponding user ID in the HashTable if it is not judged that the user is not online (the user is not online) may be the cancellation button. , Web timeout, etc.)
1. Determine whether the user is online (for user call) //////////////////////////////////////// param> /// param> ///
2. User login Event Process: Private Void Btnlogin_Click (Object sender, system.web.ui.imageClickeventargs e) {user is a custom class, which contains the login method user curuser = new user (); curuser.userid = this.username .Text.trim (); if (MyUTility.Amionline (Curuser.Userid, (HashTable) Application ["Online"])) {JScript.alert ("The login ID you use is already online! You can't log in again!" ); return;} CurUser.LoginPsw = FormsAuthentication.HashPasswordForStoringInConfigFile (this.password.Text.Trim (), "SHA1"); int ii = CurUser.Login (); StringBuilder sbPmt = new StringBuilder (); switch (ii) { Case 0: // If the login is successful, add UserID to Application ["Online"], HashTable H = Application ["Online"]; if (h == null) h = new hashtable (); h [session .SessionID] = curuser.userid; application ["online"] = h; session ["userid"] = curuser.userid; session ["usernm"] = curuser.usem; session ["rolemap"] = curuser.rolemap; Session ["LoginpsW"] = curuser.loginpsw; session ["logintime"] = datetime.now; response.redirect ("chooserole.aspx"); break; case -1: jscript.alert ("Username Error!") Break; Case -2: JScript.alert ("Secret Code error! "); Break; default: sbpmt.append (" unknown error during login! "); jscript.alert (sbpmt.tostring ()); Break;} return;
3, session_end event in Global.asax: protected void session_end (Object sender, eventargs e) {hashtable h = (havehtable) Application ["Online"]; if (h [session.sessionID]! = Null) H.Remove (Session.Session); Application ["online"] = h;} 4, in each page, you need to refresh the place, call the following code: Try {if (! "" @.Myutility.amionline (session ["UserID"]. Tostring (), ("Online"])) {// User does not have online, go to the login interface response.write ("