Ejemplo n.º 1
0
        private bool usuarioValido(string username, string password)
        {
            bool isValid = false;

            try
            {
                DataTable DataUser = new DataTable();
                DataUser = NUsuario.BuscarUsuario(username);

                DataRow row = DataUser.Rows[0];
                if (!string.IsNullOrEmpty(row["US_NombreUsuario"].ToString()))
                {
                    byte[] salt           = (byte[])row["US_Salt"];
                    byte[] pass           = (byte[])row["US_Pass"];
                    string nombreUsuario  = row["US_NombreUsuario"].ToString();
                    byte[] hashedPassword = Cryptographic.HashPasswordWithSalt(Encoding.UTF8.GetBytes(password), salt);

                    if (hashedPassword.SequenceEqual(pass))
                    {
                        isValid = true;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Usuarios o Contraseñas incorrectos", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Error);
                isValid = false;
            }

            return(isValid);


            /*   string UserForm = txtUsuario.Text;
             * string nombreUsuario = row["US_NombreUsuario"].ToString(); */
        }
        private void GuardarUSuario()
        {
            int contador = 0;

            foreach (Control ctrl in this.groupBox1.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox text = ctrl as TextBox;


                    if (text.TextLength > 3)
                    {
                        contador++;
                    }
                    else
                    {
                    }
                }
            }

            if (contador == 4)
            {
                try
                {
                    string rpta = "";

                    string Nombre        = this.txtNombre.Text;
                    string NombreUsuario = this.txtUsuario.Text;
                    string password      = this.txtContrasenha.Text;
                    string TipoUsuario   = this.comboTipoUsuario.SelectedItem.ToString();
                    int    EstadoUsuario = 1;

                    if (comboEstado.SelectedItem.ToString() == "HABILITADO")
                    {
                        EstadoUsuario = 1;
                    }
                    else
                    {
                        EstadoUsuario = 2;
                    }
                    byte[] salt           = Cryptographic.GenerateSalt();
                    var    hashedPassword = Cryptographic.HashPasswordWithSalt(Encoding.UTF8.GetBytes(password), salt);
                    rpta = NUsuario.Insertar(Nombre, NombreUsuario, salt, hashedPassword, EstadoUsuario, TipoUsuario);
                    MessageBox.Show("Usuario creado existosamente", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("El usuario ya esta creado", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    /* throw; */
                }
            }
            else
            {
                MessageBox.Show("Existen campos no llenados, o incumplen el mínimo de 4 caracteres y máx 12", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }