Global.asax.cs防注入处理

 可以通过在Global.asax.cs文件中添加过滤关键字的方法来实现 防止 sql 注入攻击(sql injection),这个方法来缘于网络非本人原创,具体性能如何本人没有做测试。代码如下。
    ///  <summary> 
    /// 当有数据时交时,触发事件 
    ///  </summary> 
    ///  <param name="sender"> </param> 
    ///  <param name="e"> </param> 
    protected void Application_BeginRequest(Object sender, EventArgs e) 
    { 
        //遍历Post参数,隐藏域除外 
        foreach (string i in this.Request.Form) 
        { 
            if (i == "__VIEWSTATE") continue; 
            this.goErr(this.Request.Form[i].ToString()); 
        } 
        //遍历Get参数。 
        foreach (string i in this.Request.QueryString) 
        { 
            this.goErr(this.Request.QueryString[i].ToString()); 
        }
    }
    ///<summary> 
    ///SQL注入过滤 
    ///  </summary> 
    ///  <param name="InText">要过滤的字符串 </param> 
    ///  <returns>如果参数存在不安全字符,则返回true </returns> 
    public bool SqlFilter(string InText) 
    { 
        string word = "and |exec |insert |select |delete |update |chr |mid |master |or |truncate |char |declare |join |cmd | |' |--";//这里加要过滤的SQL字符 
        if (InText == null) 
            return false; 
        foreach (string i in word.Split(' |')) 
        { 
            if ((InText.ToLower().IndexOf(i + " ") > -1)  | | (InText.ToLower().IndexOf(" " + i) > -1)) 
            { 
                return true; 
            } 
        } 
        return false; 
    }
    ///  <summary> 
    /// 校验参数是否存在SQL字符 
    ///  </summary> 
    ///  <param name="tm"> </param> 
    private void goErr(string tm) 
    { 
        if (SqlFilter(tm)) 
        { 
            Response.Write(" <script>window.alert('参数存在不安全字符');"+" </"+"script>"); 
        } 
    }

转载于:https://www.cnblogs.com/liuwei8278/archive/2011/03/23/1992917.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值