Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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登录中的存储过程和参数#_C#_Sql Server_Stored Procedures_Parameter Passing - Fatal编程技术网

C# C登录中的存储过程和参数#

C# C登录中的存储过程和参数#,c#,sql-server,stored-procedures,parameter-passing,C#,Sql Server,Stored Procedures,Parameter Passing,我一直在做一个项目,我对C#很陌生。我的登录是正确的,但很容易受到SQL注入攻击。这是我的代码——有人能帮我解决如何应用带参数的存储过程,使其更安全吗 protected void Button1_Click(object sender, EventArgs e) { string Cs = ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ConnectionString; using(

我一直在做一个项目,我对C#很陌生。我的登录是正确的,但很容易受到SQL注入攻击。这是我的代码——有人能帮我解决如何应用带参数的存储过程,使其更安全吗

protected void Button1_Click(object sender, EventArgs e)
{
    string Cs = ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ConnectionString;

    using(SqlConnection con=new SqlConnection(Cs))
    {
        SqlCommand cmd = new SqlCommand("Select * from Users where Username= '" + Username.Text + "' And " +
                    "Password='" + Password.Text+ "'", con);

        con.Open();

        SqlDataAdapter sda = new SqlDataAdapter(cmd);

        DataTable dt = new DataTable();
        sda.Fill(dt);

        if (dt.Rows.Count != 0)
        {
            Response.Redirect("~/Cuhome.aspx");
        }
        else
        {
            LblError.Text = "Invalid Username & Password";
        }
    }
}

谢谢

您不一定需要一个存储过程,只需使用一个正确参数化的查询即可实现“更安全”的相同目标:


但是这段代码还有一个更可怕的缺陷:你似乎在数据库表中以纯文本的形式为你的用户存储密码!这对于任何安全站点来说都是一个不重要的问题-不要用纯文本存储密码!!如果您实际存储密码,则需要哈希和salt密码。

您不一定需要存储过程,只需使用正确参数化的查询即可实现“更安全”的相同目标:


但是这段代码还有一个更可怕的缺陷:你似乎在数据库表中以纯文本的形式为你的用户存储密码!这对于任何安全站点来说都是一个不重要的问题-不要用纯文本存储密码!!如果您实际存储密码,则需要哈希和salt密码。

我不理解如何组合密码的概念。我真的很感谢你的帮助。你试过在谷歌上搜索吗?还是你想让我们帮你?看起来您正在创建一个asp.net项目。如果你创建一个MVC项目,你就可以免费获得所有这些功能,我是一个标准的、高度安全的方式。在VS中创建一个新的解决方案并选择mvc。我不理解如何组合的概念。我真的很感谢你的帮助。你试过在谷歌上搜索吗?还是你想让我们帮你?看起来您正在创建一个asp.net项目。如果你创建一个MVC项目,你就可以免费获得所有这些功能,我是一个标准的、高度安全的方式。在VS中,创建一个新的解决方案并选择mvc。此外,除了更好的安全性之外,参数化查询消除了根据类型将文本括起来(或不括起来)的需要,消除了在字符串中转义引号的需要,避免了以特定方式格式化日期字符串文本的需要,不需要十进制分隔符,通过促进执行计划缓存重用来提高性能,并生成更易于维护的更干净的代码。+1用于强类型参数,而不是。我非常感谢您的帮助。我尝试过这样做,但无法理解。非常感谢。除了更好的安全性之外,参数化查询还消除了根据类型将文本括起来(或不括起来)的需要,消除了在字符串中转义引号的需要,避免了以特定方式格式化日期字符串文本的需要,不需要十进制分隔符,通过促进执行计划缓存重用来提高性能,并生成更易于维护的更干净的代码。+1用于强类型参数,而不是。我非常感谢您的帮助。我尝试过这样做,但无法理解。非常感谢你。
protected void Button1_Click(object sender, EventArgs e)
{
    string Cs = ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ConnectionString;

    // set up query - using PARAMETERS as you always should!
    // Also: you don't seem to need the *whole* row - all the columns of "Users" - so select just what you **really need**!
    string query = "Select UserId from Users where username = @username and password = @password;";

    // put both SqlConnection *AND* SqlCommand into "using" blocks
    using (SqlConnection con=new SqlConnection(Cs))
    using (SqlCommand cmd = new SqlCommand(query, con))
    {
        // provide the parameters
        cmd.Parameters.Add("@username", SqlDbType.VarChar, 100).Value = Username.Text;
        cmd.Parameters.Add("@password", SqlDbType.VarChar, 100).Value = Password.Text;

        // use an ExecuteScalar call to get the UserId from Users - and check if it exists
        con.Open();

        object result = cmd.ExecuteScalar();

        // if we get something back --> the user with this password exists --> redirect
        if (result != null)
        {
            Response.Redirect("~/Cuhome.aspx");
        }
        else
        {
            LblError.Text = "Invalid Username & Password";
        }
    }
}