方法一:
< appSettings> < add key=safeParameters value=OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip /> < /appSettings>
其中key是 < saveParameters>后面的值为”OrderId-int32”等,其中”-“前面表示参数的名称比如:OrderId,后面的int32表示数据类型。
方法二:
在Global.asax中增加下面一段:
protected void Application_BeginRequest(Object sender, EventArgs e){ String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings[safeParameters].ToString()。Split(','); for(int i= 0 ;i < safeParameters.Length; i++){ String parameterName = safeParameters[i].Split('-')[0]; String parameterType = safeParameters[i].Split('-')[1]; isValidParameter(parameterName, parameterType); } } public void isValidParameter(string parameterName, string parameterType){ string parameterValue = Request.QueryString[parameterName]; if(parameterValue == null) return; if(parameterType.Equals(int32)){ if(!parameterCheck.isInt(parameterValue)) Response.Redirect(parameterError.aspx); } else if (parameterType.Equals(USzip)){ if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect(parameterError.aspx); } else if (parameterType.Equals(email)){ if(!parameterCheck.isEmail(parameterValue)) Response.Redirect(parameterError.aspx); } }
方法三:
使用字符串过滤类
/**//// < summary> /// 处理用户提交的请求 /// < /summary> public static void StartProcessRequest() { // System.Web.HttpContext.Current.Response.Write(< script>alert('dddd');< /script>); try { string getkeys = ; //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings[CustomErrorPage].ToString(); if (System.Web.HttpContext.Current.Request.QueryString != null) { for(int i=0;i< System.Web.HttpContext.Current.Request.QueryString.Count;i++) { getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i]; if (!ProcesssqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys],0)) { //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+?errmsg=sqlserver&sqlprocess=true); System.Web.HttpContext.Current.Response.Write(< script>alert('请勿非法提交!');history.back();< /script>); System.Web.HttpContext.Current.Response.End(); } } } if (System.Web.HttpContext.Current.Request.Form != null) { for(int i=0;i< System.Web.HttpContext.Current.Request.Form.Count;i++) { getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i]; if (!ProcesssqlStr(System.Web.HttpContext.Current.Request.Form[getkeys],1)) { //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+?errmsg=sqlserver&sqlprocess=true); System.Web.HttpContext.Current.Response.Write(< script>alert('请勿非法提交!');history.back();< /script>); System.Web.HttpContext.Current.Response.End(); } } } } catch { // 错误处理: 处理用户提交信息! } } /**//// < summary> /// 分析用户请求是否正常 /// < /summary> /// < param name=Str>传入用户提交数据< /param> /// < returns>返回是否含有sql注入式攻击代码< /returns> private static bool ProcesssqlStr(string Str,int type) { string sqlStr; if(type == 1) sqlStr = exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ; else sqlStr = '|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare; bool ReturnValue = true; try { if (Str != ) { string[] anysqlStr = sqlStr.Split('|'); foreach (string ss in anysqlStr) { if (Str.IndexOf(ss)>=0) { ReturnValue = false; } } } } catch { ReturnValue = false; } return ReturnValue; } #endregion } }
相关视频教程推荐:《C#教程》
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。