Пример #1
0
        private void btnIngresar_Click(object sender, EventArgs e)
        {
            try
            {
                string mensaje = string.Empty;
                if (txtUsername.Text.Trim() == string.Empty || txtPassword.Text == string.Empty) { mensaje += "Debe completar ambos campos."; }
                if (!string.IsNullOrEmpty(mensaje)) { throw new Exception(mensaje); }

                Usuario u = new Usuario().obtenerPorUsernameYRol(txtUsername.Text.Trim(), rolActual.idRol);
                if (u == null)
                {
                    throw new Exception("El usuario ingresado no existe.");
                }
                if (u.intentosUsuario == 3)
                {
                    throw new Exception("El usuario esta inhabilitado debido a que ha alcanzado los 3 intentos. Por favor, comuniquese con un administrador del sistema.");
                }
                if (u.passwordUsuario != EncriptadorSHA256.Encode(txtPassword.Text))
                {
                    u.intentosUsuario++;
                    u.actualizar(u);
                    txtPassword.Text = "";
                    txtPassword.Focus();
                    throw new Exception("Usuario y contraseña no coinciden, por favor, ingreselos de nuevo");
                }
                u.intentosUsuario = 0;
                u.resetearIntentos(u.idUsuario);
                MessageBox.Show(string.Format("Bienvenido, {0}.", u.usernameUsuario), "Bienvenido");
                UsuarioLogueado.usuario = u;
                this.Hide();
                Home.formHome f = new Home.formHome();
                f.ShowDialog();
                f.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }