Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#登录表单是否安全/正确?_C#_Sql_Security - Fatal编程技术网

C#登录表单是否安全/正确?

C#登录表单是否安全/正确?,c#,sql,security,C#,Sql,Security,今年我正在为C#(学校)做最后一个项目,我上次在这个网站上得到帮助时承诺,我会确保我的SQL是安全的,我会确保我的应用程序是安全的。有人能看看我的登录屏幕,告诉我这是否是一个正确和安全的方式吗 我首先通过Program.cs打开我的主MDI容器: private void Form1_Load(object sender, EventArgs e) { fL.ShowDialog(); } 然后,此登录表单显示: string User = txtUser.Te

今年我正在为C#(学校)做最后一个项目,我上次在这个网站上得到帮助时承诺,我会确保我的SQL是安全的,我会确保我的应用程序是安全的。有人能看看我的登录屏幕,告诉我这是否是一个正确和安全的方式吗

我首先通过Program.cs打开我的主MDI容器:

private void Form1_Load(object sender, EventArgs e)
    {
        fL.ShowDialog();
    }
然后,此登录表单显示:

string User = txtUser.Text;
        string Pw = txtPw.Text;
        int Correct = clDatabase.login(User, Pw);

        if (Correct == 1)
        {
            this.Hide();
        }
        else
        {
            MessageBox.Show("De gegevens die u heeft ingevult kloppen niet", "Fout!"); //Above means your input is not correct
        }
和在clDatabase.login中

public static int login(string GebruikersnaamI, string WachtwoordI)
    {
        int correct = 0;
        SqlConnection Conn = new SqlConnection(clStam.Connstr);
        Conn.Open();
        using (SqlCommand StrQuer = new SqlCommand("SELECT * FROM gebruiker WHERE usernm=@userid AND userpass=@password", Conn))
        {
            StrQuer.Parameters.AddWithValue("@userid", GebruikersnaamI);
            StrQuer.Parameters.AddWithValue("@password", WachtwoordI);
            SqlDataReader dr = StrQuer.ExecuteReader();
            if (dr.HasRows)
            {
                correct = 1;
                MessageBox.Show("loginSuccess");
            }
            else
            {
                correct = 2;
                //invalid login
            }
        }
        Conn.Close();
        return correct;
    }
LoginSAccess对话框仅用于调试目的 这安全吗?这是登录表单的正确方式吗

编辑更新的代码登录表单:

private void button1_Click(object sender, EventArgs e)
    {
        ErrorProvider EP = new ErrorProvider();

        if (txtUser.Text == string.Empty || txtPw.Text == string.Empty)
        {
            if (txtUser.Text == string.Empty)
                txtUser.BackColor = Color.Red;
            if (txtPw.Text == string.Empty)
                txtPw.BackColor = Color.Red;

            MessageBox.Show("Er moet wel iets ingevuld zijn!", "Fout");
        }
        else
        {
            string User = txtUser.Text;
            string Pw = txtPw.Text;
            Boolean Correct = clDatabase.login(User, Pw);

            if (Correct == true)
            {
                this.Hide();
            }
            else
            {
                MessageBox.Show("Deze combinatie van username en password is niet bekend", "Fout!");
            }
        }
    }

clDatabase:
public static Boolean login(string GebruikersnaamI, string WachtwoordI)
    {
        Boolean correct = false;
        using (SqlConnection Conn = new SqlConnection(clStam.Connstr))
        {
            Conn.Open();
            using (SqlCommand StrQuer = new SqlCommand("SELECT * FROM gebruiker WHERE usernm=@userid AND userpass=@password", Conn))
            {
                StrQuer.Parameters.AddWithValue("@userid", GebruikersnaamI);
                StrQuer.Parameters.AddWithValue("@password", WachtwoordI);
                using (SqlDataReader dr = StrQuer.ExecuteReader())
                {
                    if (dr.HasRows)
                    {
                        correct = true;
                    }
                    else
                    {
                        correct = false;
                        //invalid login
                    }
                }
            }
            Conn.Close();
        }
        return correct;
    }

就SQL注入而言,它是安全的,因为您正在传递参数不要将密码存储为纯文本,而是存储其哈希值


请参阅:

如果他们看不到您的源代码,那么这是安全的,但您应该定义使用安全的含义。我还建议将using也用于连接和读卡器。顺便问一下,为什么用整数返回码而不是简单的布尔值?如果我还有时间,我会加上correct=3;若要检查其中一个框是否为空,并在无法连接到数据库时返回不同的选项或不同的错误。在login()中,我不会处理无效输入,不要混淆责任。它必须验证凭据,而不是其他。验证用户输入是调用函数的责任(顺便说一句,您也有bool?)。文字常量很难遵循(如果您有一长串错误代码…请使用枚举)。如何将其存储为哈希?编辑:谢谢@StrahBehry,从问题中的链接阅读,也从这里阅读。如果我对其进行哈希运算,如何将其与数据库中的密码进行比较?我一定要用同样的方法把它弄碎吗?老实说,我不明白它是如何工作的:S,用你的答案中所示的方法对密码进行哈希运算后,我不知道该去哪里。还更新了问题中当前的代码。@StrahBehry,是的。首先,您将对密码进行散列,然后将散列值存储在数据库中,稍后,当任何用户返回时,您将获得密码,使用相同的方法对其进行散列,并在数据库中比较散列值。这将是单向散列。这样,您将永远不会知道用户的实际密码。(这是一件好事)。可能是,只是可能是,这对学校项目来说太多了,但您可以随时寻找存储密码的更好/标准实践。