Prevent repeated User Logon

Source: Internet
Author: User

The function completed in this example is to prevent repeated logon! If the user has logged on, a prompt box is displayed when the user logs on again!

Implementation idea: After a user successfully logs on, the user login information is stored in application ["online"] of hashtable type. The key value is sessionid and the value is the user ID; when the user logs out, the session is called. abandon; in global. in the sessionend event in asax, the user ID is deleted from hashtable. When a user accesses the page, check whether there is a corresponding user ID in hashtable. If not, judge whether the user is offline (the reason why the user is offline may be due to the Logout button, webpage timeout, etc)

1. functions used to determine whether a user is online (called by the user) in a public class)
/// <Summary>
/// Determine whether the user struserid is included in hashtable H
/// </Summary>
/// <Param name = "struserid"> </param>
/// <Param name = "H"> </param>
/// <Returns> </returns>
Public static bool amionline (string struserid, hashtable H)
{
If (struserid = NULL)
Return false;

// Continue to determine whether the user has logged on
If (H = NULL)
Return false;

// Determine whether the user exists in the hash table
Idictionaryenumerator e1 = H. getenumerator ();
Bool flag = false;
While (e1.movenext ())
{
If (e1.value. tostring (). compareto (struserid) = 0)
{
Flag = true;
Break;
}
}
Return flag;
}

2. handling user logon events:
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 ("your logon ID is already online! You cannot log on 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 logon is successful, add the userid to application ["online "].
Hashtable H = (hashtable) application ["online"];
If (H = NULL)
H = new hashtable ();
H [session. sessionid] = curuser. userid;
Application ["online"] = h;

Session ["userid"] = curuser. userid;
Session ["usernm"] = curuser. usernm;
Session ["rolemap"] = curuser. rolemap;
Session ["loginpsw"] = curuser. loginpsw;
Session ["logintime"] = datetime. now;
Response. Redirect ("chooserole. aspx ");
Break;
Case-1:
JScript. Alert ("the user name is incorrect! ");
Break;
Case-2:
JScript. Alert ("Incorrect password! ");
Break;
Default:
Sbpmt. append ("Unknown error occurred 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 = (hashtable) application ["online"];

If (H [session. sessionid]! = NULL)
H. Remove (session. sessionid );

Application ["online"] = h;
}

4. to refresh a page, call the followingCode:
Try
{
If (! Common. myutility. amionline (session ["userid"]. tostring (), (hashtable) application ["online"])
{
// The user is not online. Go to the logon page.
Response. Write ("<SCRIPT> parent.doc ument. Location. href = 'login. aspx '; </SCRIPT>"); // used when a framework is available
// Response. Redirect ("login. aspx"); // No framework
Return;
}
}
Catch
{
// When the session expires, go to the logon page
Response. Write ("<SCRIPT> parent.doc ument. Location. href = 'login. aspx '; </SCRIPT>"); // used when a framework is available
// Response. Redirect ("login. aspx"); // No framework
Return;
}

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.