一:一個方法
/// <summary>
///SQL注入過濾
/// </summary>
/// <param name="InText">要過濾的字串</param>
/// <returns>如果參數存在Unsafe 字元,則返回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;
}
二:global.ascx 中的一個方法
/// <summary>
/// 當有資料時交時,觸發事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_BeginRequest(Object sender, EventArgs e)
{
//遍曆Post參數,隱藏欄位除外
foreach (string str_t in this.Request.Form)
{
if (str_t == "__VIEWSTATE") continue;
this.goErr(this.Request.Form[str_t].ToString());
}
//遍曆Get參數。
foreach (string str_t in this.Request.QueryString)
{
this.goErr(this.Request.QueryString[str_t].ToString());
}
}
三:global.asax中的另外一方法
/// <summary>
/// 校正參數是否存在SQL字元
/// </summary>
/// <param name="tm"></param>
private void goErr(string tm)
{
if (wenlong.function.func.SqlFilter2(tm))
this.Response.Redirect("/error");
//Response.Redirect();
}