我正在设计一个网站,用户在其中指定一个帐户ID(确切地说,必须为8位数字),以便查找与该帐户相关联的“开票日期”。我使用过一个asp.net正则表达式验证器来防止用户输入字符。我还向该文本框附加了必填字段验证器。
我已经阅读了来自其他stackoverflow问题的SQL Injection攻击,但是没有发现与使用验证程序保护查询有关的任何内容。
设置了这些验证器后,我是否有任何理由担心sql注入攻击?还有什么我需要做(或应该做)的操作,以防止恶性用户滥用此用户输入。
这是我的C#代码,用于SQL查询,并在下拉列表中填充与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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句