Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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# 我可以在用户输入上使用asp.net验证程序来避免sql注入攻击吗?_C#_Asp.net_Sql Server - Fatal编程技术网

C# 我可以在用户输入上使用asp.net验证程序来避免sql注入攻击吗?

C# 我可以在用户输入上使用asp.net验证程序来避免sql注入攻击吗?,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我正在设计一个网站,其中用户指定一个帐户ID(必须是8位数字,准确地说),以便查找与该帐户相关的计费日期。我使用了asp.net正则表达式验证程序来防止用户输入字符。我还为此文本框附加了一个必需的字段验证器 我从其他stackoverflow问题中了解了SQL注入攻击,但我没有遇到任何与使用验证器保护查询有关的问题 设置了这些验证器之后,我有什么理由担心sql注入攻击吗?我还需要(或应该)做些什么来防止恶意用户滥用此用户输入 下面是我的SQL查询C#代码,并用与AccountID关联的票据周期日

我正在设计一个网站,其中用户指定一个帐户ID(必须是8位数字,准确地说),以便查找与该帐户相关的计费日期。我使用了asp.net正则表达式验证程序来防止用户输入字符。我还为此文本框附加了一个必需的字段验证器

我从其他stackoverflow问题中了解了SQL注入攻击,但我没有遇到任何与使用验证器保护查询有关的问题

设置了这些验证器之后,我有什么理由担心sql注入攻击吗?我还需要(或应该)做些什么来防止恶意用户滥用此用户输入

下面是我的SQL查询C#代码,并用与AccountID关联的票据周期日期填充下拉列表:

string sqlCommandString = "SELECT StatementDate AS StateDate FROM dbTable " + 
    "WHERE AccountID = '" + AccountID + "' ORDER BY StatementDate DESC";

string ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
using (SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection))
{
    sqlConnection.Open();
    DropDownList_StatementDate.DataSource = sqlCommand.ExecuteReader();
    DropDownList_StatementDate.DataBind();
}
下面是我使用的正则表达式验证器:

<asp:RegularExpressionValidator
    ID="RegExpVal_AccountID"
    runat="server"
    ErrorMessage="Must be 8 digits"
    ValidationExpression="^\d{8}$"
    ControlToValidate="TextBox_AccountID"
    CssClass="ValidatorStyle"
    Display="Dynamic">        
</asp:RegularExpressionValidator>


谢谢。

只需使用参数化查询(防止SQL注入攻击的唯一安全方法):


你做错了。清理输入,而不是验证输入。检查达林的答案。
string sqlCommandString = "SELECT StatementDate AS StateDate FROM dbTable " + 
    "WHERE AccountID = @AccountID ORDER BY StatementDate DESC";

string ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
using (SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection))
{
    sqlConnection.Open();
    sqlCommand.Parameters.AddWithValue("@AccountID", AccountID);
    DropDownList_StatementDate.DataSource = sqlCommand.ExecuteReader();
    DropDownList_StatementDate.DataBind();
}