1、新建Global.asax文件。
2、到场以下代码:
void Application_BeginRequest(object sender, EventArgs e) { bool result = false; if (Request.RequestType.ToUpper() == "POST") { //post体式格局的我就不写了。 } else { result = ValidUrlGetData(); } if (result) { Response.Write("您提交的数据有歹意字符!"); Response.End(); } } /// <summary> /// 猎取QueryString中的数据 /// </summary> public static bool ValidUrlGetData() { bool result = false; for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++) { result = Validate(HttpContext.Current.Request.QueryString[i].ToString()); if (result) { break; }//假如检测存在破绽 } return result; } public static string []strs = new string[] {"select","drop","exists","exec","insert","delete","update","and","or","user" };//此处我随意加了几个,人人能够多加点哈。 public static bool Validate(string str) { for (int i = 0; i < strs.Length; i++) { if (str.IndexOf(strs[i]) != -1) { return true; break; } } return false; }
以上就是防备SQL注入的ASP.NET要领实例剖析的细致内容,更多请关注ki4网别的相干文章!