过滤网址和输入框中的特殊字符,防止sql注入

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.HtmlControls;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10.   
  11. /// <summary>  
  12. ///cedar 的摘要说明  
  13. /// </summary>  
  14. public class cedar:IHttpModule  
  15. {  
  16.     public cedar()  
  17.     {  
  18.         //  
  19.         //TODO: 在此处添加构造函数逻辑  
  20.         //  
  21.     }  
  22.     public void Dispose()  
  23.     {  
  24.   
  25.     }  
  26.   
  27.     public void Init(HttpApplication application)  
  28.     {  
  29.         application.AcquireRequestState += new EventHandler(application_AcquireRequestState);  
  30.     }  
  31.     private void application_AcquireRequestState(object sender, EventArgs e)  
  32.     {  
  33.         HttpContext content = ((HttpApplication)sender).Context;  
  34.         try  
  35.         {  
  36.             string sqlErrorPage = "default.html";//转到默认页面  
  37.             string keyValue = string.Empty;  
  38.             string requestUrl = content.Request.Path.ToString();  
  39.             if (content.Request.QueryString != null)  
  40.             {  
  41.                 foreach (string val in content.Request.QueryString)  
  42.                 {  
  43.                    keyValue= content.Server.UrlDecode(content.Request.QueryString[val]);  
  44.                    if (!processSqlStr(keyValue))  
  45.                    {  
  46.                        content.Response.Write("您访问的页面发生错误,此问题我们已经记录并尽快改善,请稍后再试。<br><a href=""+sqlErrorPage+"" mce_href=""+sqlErrorPage+"">转到首页</a>");  
  47.                        content.Response.End();  
  48.                        break;  
  49.                    }  
  50.                 }  
  51.             }  
  52.             if (content.Request.Form != null)  
  53.             {  
  54.                 foreach(string val in content.Request.Form)  
  55.                 {  
  56.                     keyValue = content.Server.HtmlDecode(content.Request.Form[val]);  
  57.                     if (keyValue == "_ViEWSTATE") continue;  
  58.                     if (!processSqlStr(keyValue))  
  59.                     {  
  60.                         content.Response.Write("您访问的页面发生错误,此问题我们已经记录并尽快改善,请稍后再试。");  
  61.                         content.Response.End();  
  62.                         break;  
  63.                     }  
  64.                 }  
  65.             }  
  66.         }  
  67.         catch (Exception ex)  
  68.         {  
  69.         }  
  70.     }  
  71.     private bool processSqlStr(string str)  
  72.     {  
  73.         bool returnValue = true;  
  74.         try  
  75.         {  
  76.             if (str.Trim() != "")  
  77.             {  
  78.                 //取得webconfig中过滤字符串  
  79.                 string sqlStr = ConfigurationManager.AppSettings["FilterSql"].Trim();  
  80.                 //string sqlStr = "declare |exec|varchar |cursor |begin |open |drop |creat |select |truncate";  
  81.                 string[] sqlStrs = sqlStr.Split('|');  
  82.                 foreach (string ss in sqlStrs)  
  83.                 {  
  84.                     if (str.ToLower().IndexOf(ss) >= 0)  
  85.                     {  
  86.                         sqlStr = ss;  
  87.                         returnValue = false;  
  88.                         break;  
  89.                     }  
  90.                 }  
  91.             }  
  92.         }  
  93.         catch  
  94.         {  
  95.             returnValue = false;  
  96.         }  
  97.         return returnValue;  
  98.     }  
  99. }  
  100. 在web.config中添加以下:

    <appSettings>
      <add key="FilterSql" value="declare |exec|varchar |cursor |begin |open |drop |creat |select |truncate "/>
     </appSettings>

    <httpModules>
       <add type="cedar" name="cedar"/>
      </httpModules>

转载于:https://www.cnblogs.com/ITzhangyunpeng/p/9154887.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值