Specifying Maximum Login Attempts - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Specifying Maximum Login Attempts

PriapusPriapus Posts: 36Member
Hi,
I've got a login form that authenticates a user against a database. I'm trying to implement a function whereby if a user enters 3 invalid passwords in succession, a message is displayed and the application shuts down. I'm using a for loop 2 achieve this(shown in bold text), but i can't get it to work. Any suggestions for how i should implement this bit of functionality? Below is the login function for the form.
[code syntax = c#]
public partial class LoginForm : Form
{
private LoginObject aLoginObject;
public LoginForm()
{
InitializeComponent();
aLoginObject = new LoginObject();
}

private void LoginForm_Load(object sender, EventArgs e)
{
this.BackColor = Color.Azure;
}

private void btnEnter_Click(object sender, EventArgs e)
{
try
{
string resultMsg = "";
byte passConfirm = 0;
int loginAttempts = 0;

// validate user input
// if no input from the user, display error message
if (txtPassword.Text.Trim().Equals(""))
{
MessageBox.Show("You must enter a password!");
txtPassword.Focus();
txtPassword.Text = "";
}
else
{
passConfirm = aLoginObject.login(txtPassword.Text, ref resultMsg);
if(passConfirm == 1)
{
// Success
// MessageBox.Show(resultMsg);
// Enter the application.Show the main menu tree view
TreeViewMenuForm aTreeViewMenuForm;
aTreeViewMenuForm = new TreeViewMenuForm();
aTreeViewMenuForm.Show();
}
else if (passConfirm == 2)
{
// Invalid password
MessageBox.Show(resultMsg);
txtPassword.Focus();
txtPassword.Text = "";
}
else
{
// 255 DAtabase error
MessageBox.Show(resultMsg);
}
// if user enters incorrect password 3 times
// the application will shutdown
[b]for (int i = 0; i < 3; i++)
{
loginAttempts = loginAttempts + 1;
if (loginAttempts > 3)
{
MessageBox.Show("You do not have access to this wallet");
Application.Exit();
}[/b]
}

}
}// end try

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}// end of btnEnter_click() method

private void btnClear_Click(object sender, EventArgs e)
{
txtPassword.Text = "";
txtPassword.Focus();
}

private void mnuExit_Click(object sender, EventArgs e)
{
Application.Exit();
}

}
[/code]

Comments

  • girlkulitgirlkulit Posts: 3Member
    try to put int loginAttempts = 0; after the line of aLoginObject = new LoginObject();

    then in your if statement, modify the block of your else if to:

    else if (passConfirm == 2)
    {
    loginAttempts = loginAttempts + 1;
    if (loginAttempts < 3)
    {
    // Invalid password
    MessageBox.Show(resultMsg);
    txtPassword.Focus();
    txtPassword.Text = "";
    }
    else
    {
    MessageBox.Show("You do not have access to this wallet");
    Application.Exit();
    }
    }

    hope this helps,
  • girlkulitgirlkulit Posts: 3Member
    post deleted.
  • PriapusPriapus Posts: 36Member
    Your suggestion worked!! Thank you so very much
Sign In or Register to comment.