Validating Login

Asked

Viewed 1,675 times

0

I’m trying to log in, but I’m not able to do one thing.

I first check that the user and password fields are filled in. So far so good. I then check that the password and the user are correct. And in case you’re correct he’ll open a new form.

However, I would like to make sure that when I type in the password field 123 (which would be a correct password) it does not open the form but runs the last if for me to register a new password. And then yes, the form would open when I logged in with the new password.

I’m kind of lost and I don’t know if I used the ifs and the elses correctly.

My Code:

private void btnentrar_Click(object sender, EventArgs e)
{
    if ((txtLogin.Text == "") || (txtsenha.Text == ""))
    {
        MessageBox.Show("Digite Usuário e Senha!");
        txtLogin.Focus();

    }
    else
    {
        clnlogin login = new clnlogin();
        OracleDataReader objDados;
        objDados = login.ListarLogin(txtLogin.Text);
        if (objDados.Read())
        {
            login.Usuario = objDados["usuario"].ToString();
            login.Senha = objDados["senha"].ToString();
            if (txtsenha.Text != login.Senha)
            {
                MessageBox.Show(" Senha Inválida", "ocorreu um Erro ao autenticar", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtsenha.Clear();
                txtsenha.Focus();
            }

            else
            {
                frmPrincipal x = new frmPrincipal();
                this.Visible = false;
                MessageBox.Show("Bem Vindo ao Sistema" + "," + txtLogin.Text);
                x.Show();
            }
        }
        else
        {
            MessageBox.Show("Usuário Incorreto!");
            txtLogin.Clear();
            txtsenha.Clear();
            txtLogin.Focus();
        }

        if ((txtsenha.Text == "123"))
        {
            MessageBox.Show("Cadastre sua Nova Senha!");
            txtsenha.Enabled = false;
            txtLogin.Enabled = false;
            lblnovasenha.Visible = true;
            txtnovasenha.Visible = true;
            btnsalvar.Visible = true;
            btnCancelar.Visible = false;
            btnentrar.Visible = false;
            txtnovasenha.Focus();
        }
    }
}

  • Just put the if of the password at the beginning of the first Else.

2 answers

1

Avoid using many nested if’s Else, assume that if you have many, there is a high probability that your code has logic error.

I didn’t test it, but try it like this:

private void btnentrar_Click(object sender, EventArgs e)
    {
        if ((txtLogin.Text == "") || (txtsenha.Text == ""))
        {
            MessageBox.Show("Digite Usuário e Senha!");
            txtLogin.Focus();
            return;
        }

        if ((txtsenha.Text == "123"))
        {
            MessageBox.Show("Cadastre sua Nova Senha!");
            txtsenha.Enabled = false;
            txtLogin.Enabled = false;
            lblnovasenha.Visible = true;
            txtnovasenha.Visible = true;
            btnsalvar.Visible = true;
            btnCancelar.Visible = false;
            btnentrar.Visible = false;
            txtnovasenha.Focus();
            return;
        }

        clnlogin login = new clnlogin();
        OracleDataReader objDados;
        objDados = login.ListarLogin(txtLogin.Text);
        if (!objDados.Read())
        {
            MessageBox.Show("Usuário Incorreto!");
            txtLogin.Clear();
            txtsenha.Clear();
            txtLogin.Focus();
            return;
        }

        login.Usuario = objDados["usuario"].ToString();
        login.Senha = objDados["senha"].ToString();
        if (txtsenha.Text != login.Senha)
        {
            MessageBox.Show(" Senha Inválida", "ocorreu um Erro ao autenticar", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtsenha.Clear();
            txtsenha.Focus();
            return;
        }

        frmPrincipal x = new frmPrincipal();
        this.Visible = false;
        MessageBox.Show("Bem Vindo ao Sistema" + "," + txtLogin.Text);
        x.Show();
    }
  • 1

    Evite usar muitos if's else aninhado Take that advice to life. For the record, Linus Torvalds has a habit of spending hours writing personal insults to all those who submit code to the Linux Kernel with more than four levels of indentation. If’s nested then receive a whole "affection and attention" special from him.

1

If you report that the user or password specifically is incorrect, it is easy for someone malicious to try to hack into your application. I made some changes in order to reduce code.

private void btnentrar_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(txtLogin.Text) || string.IsNullOrEmpty(txtsenha.Text))
    {
        MessageBox.Show("Digite Usuário e Senha!");
        txtLogin.Focus();
        return;
    }

    if (txtsenha.Text == "123")
    {
        MessageBox.Show("Cadastre sua Nova Senha!");
        txtsenha.Enabled = false;
        txtLogin.Enabled = false;
        lblnovasenha.Visible = true;
        txtnovasenha.Visible = true;
        btnsalvar.Visible = true;
        btnCancelar.Visible = false;
        btnentrar.Visible = false;
        txtnovasenha.Focus();
        return;
    }

    clnlogin login = new clnlogin();
    OracleDataReader objDados;
    objDados = login.ListarLogin(txtLogin.Text);

    login.Usuario = objDados["usuario"].ToString();
    login.Senha = objDados["senha"].ToString();
    if (txtsenha.Text != login.Senha || !objDados.Read())
    {
        MessageBox.Show("Usuário ou Senha inválido", "ocorreu um Erro ao autenticar", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtLogin.Clear();
        txtsenha.Clear();
        txtLogin.Focus();
        return;
    }

    frmPrincipal x = new frmPrincipal();
    this.Visible = false;
    MessageBox.Show("Bem Vindo ao Sistema" + "," + txtLogin.Text);
    x.Show();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.