Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
I have a website where a user has 5 maximum login attempts to login into the website. If the user has crossed the particular number of attempts then the user will be locked. Now the default time to unlock the user is 24 hours . The task is that I need reduce the time to unlock the user to 20 minutes. Can anyone help me to do this task. The below is the code to lock the user if the user the crossed the maximum number of attempts.

private int _maxLoginAttempts = 5;
 protected void btnLogin_Click(object sender, EventArgs e)
    {
        bool loginOK = false;
        bool approved = false;
        bool maxAttempsReached = false;

        UserDetailService usersService = new UserDetailService();
        ContactService contactsService = new ContactService();
        UserDetail currentUser = null;

        //Get user by Email
        Contact currentContact = contactsService.GetByEmailAddress(uxUserName.Text);

        if (ApplicationCacheControl.UserLoginAttempts().ContainsKey(uxUserName.Text))
        {
            ApplicationCacheControl.UserLoginAttempts()[uxUserName.Text] += 1;

            if (ApplicationCacheControl.UserLoginAttempts()[uxUserName.Text] >= _maxLoginAttempts)
            {
                maxAttempsReached = true;

                try
                {
                    //Audit lockout
                    //AuditLogManager.RecordUserAction(ActivityAuditLog.ApplicationActivityTypeEnum.PublicUserLockedOut, -1, null,
                    //    "User", "Max. number of logins attempted for " + uxUserID.Text, true);
                }
                catch { }
            }
        }
        else
        {
            ApplicationCacheControl.UserLoginAttempts().Add(uxUserName.Text, 1);
        }
        if (maxAttempsReached)
        {
            phMaxLogins.Visible = true;
            phLoginControls.Visible = false;

            if (OnLoginFailed != null)
            {
                EventArgs newArgs = new EventArgs();
                OnLoginFailed(this, newArgs);
            }
        }
        else if (currentContact != null)
        {
            if (currentContact.UserDetails != null)
            {
                currentUser = currentContact.UserDetails.FirstOrDefault();

                if (currentUser != null)
                {
                    CDS.Framework.Library cdsLibrary = new CDS.Framework.Library(Common.ApplicationName);
                    
                    if (currentUser.Password == cdsLibrary.EncryptString(uxPassword.Text, true))
                    {
                        loginOK = true;

                        if (ApplicationCacheControl.UserLoginAttempts().ContainsKey(uxUserName.Text))
                        {
                            ApplicationCacheControl.UserLoginAttempts()[uxUserName.Text] = 0;
                        }
                        if (currentUser.UserStatusTypeID == 2)
                        {
                            approved = true;

                            currentUser.LastLoggedIn = DateTime.Now;
                            usersService.Save(currentUser);
                        }
                    }
                }
            }
        }
Posted
Updated 2-Jul-15 3:59am
v2
Comments
F-ES Sitecore 2-Jul-15 11:54am    
Step through your code in the debugger until you find the bit that actually sets the timeouts as it isn't in the bit you've posted.

http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn

1 solution

This section looks like a traditional event being fired.
C#
if (OnLoginFailed != null)
    {
        EventArgs newArgs = new EventArgs();
        OnLoginFailed(this, newArgs);
    }

Somewhere else in your code might be the declaration of the event, it might look like this:
C#
public event EventHandler OnLoginFailed;

Somewhere else (maybe elsewhere in the project/solution), there will probably be a method to handle this event (subscribe to it), and a wire-up to subscribe the method to the event... Maybe something like this:
handling method:
C#
private void LockUser(object sender, EventArgs e) 
      {
         // This code runs to lock the user for a period of time...
      }

subscription/wireup
C#
someObject.OnLoginFailed += new EventHandler(LockUser);

The code you have posted doesn't lock the user - it raises an event to tell something else to lock the user. Your task is to find that subscribing method (if it's within your codebase!) and make appropriate modifications there. Good luck!

Also take a look at: msdn article on C# events
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900