回复:.net里面如何防注入!(在线等)
搞定!!!整站通用
/// <summary>
/// 校验参数是否存在SQL字符
/// </summary>
/// <param name="tm"></param>
private void goErr(string tm)
{
if (SqlFilter2(tm))
{
Response.Write("<script>alert('你输入的字符串中包含非法字符!')</script>");
Response.Redirect("Error/errStr.htm");
}
}
/// <summary>
///SQL注入过滤
/// </summary>
/// <param name="InText">要过滤的字符串</param>
/// <returns>如果参数存在不安全字符,则返回true</returns>
public static bool SqlFilter2(string InText)
{
string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join|'";
if (InText == null)
return false;
foreach (string str_t in word.Split('|'))
{
if ((InText.ToLower().IndexOf(str_t + " ") > -1) || (InText.ToLower().IndexOf(" " + str_t) > -1) ||
(InText.ToLower().IndexOf(str_t) > -1))
{
return true;
}
}
return false;
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
foreach(string str in this.Request.Form)
{
if(str == "__VIEWSTATE") continue;
this.goErr(this.Request.Form[str].ToString());
}
//遍历Get参数。
foreach (string str_t in this.Request.QueryString)
{
this.goErr(this.Request.QueryString[str_t].ToString());
}
}