这是一个很好的登录和验证用户对数据库的做法

本文关键字:用户 验证 数据库 登录 很好 一个 | 更新日期: 2023-09-27 18:05:08

我想问我所做的是否被认为是登录环境的良好实践,我正在创建一个SaaS,希望您对登录服务器进行身份验证,以便使用我正在创建的软件,但我想检查我所采用的方法是否正确,所以我将在这里发布我所做的,以便看到您对它的看法!提前感谢大家的意见。

private void AcceptButton_Click(object sender, EventArgs e)
    {
        bool loginSuccesful = false;
        if (string.IsNullOrEmpty(usernameTextBox.Text))
        {
            MessageBox.Show("Por favor introduzca su Usuario.");
            usernameTextBox.Focus();
        }
        else
        {
            try
            {
                string connectionString = ConfigurationSettings.AppSettings["IntegraTDB"].ToString().Trim();
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from dbo.Users where Username = @Username and Password = @Password", con);
                cmd.Parameters.AddWithValue("@Username", usernameTextBox.Text);
                cmd.Parameters.AddWithValue("@Password", passwordTextBox.Text);                    
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    loginSuccesful = true;
                }
                dr.Close();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error de autenticación: " + ex.Message);
            }
            if (loginSuccesful == true)
            {
                role_Extraction();
                IIdentity identity;
                identity = new GenericIdentity(usernameTextBox.Text);
                IPrincipal principal = new GenericPrincipal(identity, _roles);
                Thread.CurrentPrincipal = principal;
                MessageBox.Show("Bienvenido a IntegraT!");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Usuario y/o Contraseña incorrectos.");
            }
        }
    }

这是一个很好的登录和验证用户对数据库的做法

上面看起来您可能在数据库中存储了纯文本密码。在我看来,这是应该避免的。至少应该使用某种形式的哈希。

更好的方法是利用诸如活动目录之类的身份验证解决方案。