How to prevent repeated login attempts

Source: Internet
Author: User
Implementation ideas:

After a user logs on successfully, the user logon information is stored in hashtable application ["online"]. The key value is sessionid and the value is user ID. When the user logs off, call session. abandon; in global. in the sessionend event in asax, the user ID is deleted from hashtable. When the user accesses the page, the user cannot have the corresponding user ID in hashtable. If not, the user is judged to be offline.

 

1. functions used to determine whether a user can be online (called by the user)

 

Code

/** // <Summary>
/// Determine whether the struserid can be included in hashtable H.
/// </Summary>
/// <Param name = "struserid"> </param>
/// <Param name = "H"> </param>
/// <Returns> </returns>
Public static bool amionline (string strusername, hashtable H)
{
If (strusername = NULL)
Return false;

// Continue to determine if 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 (strusername) = 0)
{
Flag = true;
Break;
}
}
Return flag;

}

 

 

2. handling user logon events:

Code

Private void btnlogin_click (Object sender, system. Web. UI. imageclickeventargs E)
{
// User is a custom class, which contains the login Essentials
User curuser = new user ();
Curuser. Username = This. username. Text. Trim ();

If (myutility. amionline (curuser. username, (hashtable) application ["online"])
{

Response. Write ("<SCRIPT> alert ('the logon ID you are using is already online! You cannot log on again! '); </SCRIPT> ");

Return;

}

Else

{
Hashtable H = (hashtable) application ["online"];

If (H = NULL)

H = new hashtable ();

H [session. sessionid] = curuser. Username;

Application ["online"] = h;

}

 

}

 

 

3. session_end event in global. asax:

Code

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;
}

 

According to the online data, the key is to refresh the idea!

<Move against the water, move back if you do not enter>

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.