Validating Login

0

I'm trying to login but I'm not able to do one thing.

First check that the user and password fields are filled in. So far so good. Then I verify that the password and the user are correct. And if it is correct, it will open a new form.

But I wanted to make sure that when I typed in the password field 123 (which would be a correct password) it would not open the form, but would have it execute the last if so I would register a new password. And yes, the form would open when I log in with the new password.

I'm kind of lost and I do not know if I used ifs and 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();
        }
    }
}
    
asked by anonymous 09.10.2016 / 03:07

2 answers

0

Avoid using many if's else nested, assuming that if you have many, there is a high probability that your code will have logic error.

I did not test, but try 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();
    }
    
09.10.2016 / 07:21
0

If you report that the user or password is specifically incorrect, it is easy for someone who is malicious to attempt to hack into your application. I made some changes 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();
}
    
26.07.2017 / 15:14