 悬赏园豆:10
                [已关闭问题] 
            
                    关闭于 2013-09-11 08:56
                悬赏园豆:10
                [已关闭问题] 
            
                    关闭于 2013-09-11 08:56
                 
        我在使用一个通用防注入的类。这个类就是过滤一些关键字,但是我新闻里面如果有这些关键字就会“误杀”。请问有没有办法解决这个问题!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class cedar : IHttpModule
 {
 public void Dispose()
 {
 throw new NotImplementedException();
 }
public void Init(HttpApplication context)
 {
 context.AcquireRequestState += new EventHandler(application_AcquireRequestState);
 }
 private void application_AcquireRequestState(object sender, EventArgs e)
 {
 HttpContext content = ((HttpApplication)sender).Context;
 try
 {
 string sqlErrorPage = "sqlErrorPage.html";//转到默认页面 
 string keyValue = string.Empty;
 string requestUrl = content.Request.Path.ToString();
 if (content.Request.QueryString != null)
 {
 foreach (string val in content.Request.QueryString)
 {
 keyValue = content.Server.UrlDecode(content.Request.QueryString[val]);
 if (!processSqlStr(keyValue))
 {
 content.Response.Write("<script>alert('请不要提交非法字符')</script>");
 content.Response.End();
 break;
 }
 }
 }
 if (content.Request.Form != null)
 {
 foreach (string val in content.Request.Form)
 {
 keyValue = content.Server.HtmlDecode(content.Request.Form[val]);
 if (keyValue == "_ViEWSTATE") continue;
 if (!processSqlStr(keyValue))
 {
 content.Response.Write("<script>alert('请不要提交非法字符')</script>");
 content.Response.End();
 break;
 }
 }
 }
 }
 catch (Exception ex)
 {
 }
 }
private bool processSqlStr(string str)
 {
 bool returnValue = true;
 try
 {
 if (str.Trim() != "")
 {
 //取得webconfig中过滤字符串 
 // string sqlStr = ConfigurationManager.AppSettings["FilterSql"].Trim();
 string sqlStr = "'|declare|exec|varchar|cursor|begin|open|drop|creat|select|truncate|Execute";
 string[] sqlStrs = sqlStr.Split('|');
 foreach (string ss in sqlStrs)
 {
 if (str.ToLower().IndexOf(ss) >= 0)
 {
 sqlStr = ss;
 returnValue = false;
 break;
 }
 }
 }
 }
 catch
 {
 returnValue = false;
 }
 return returnValue;
 }
 }
把文本中使用的这些字符转义以后再存到数据库去
你过滤单引号和--就OK了。。
但是比如你一篇文章里面有 单引号 咋办呢?
@hamigua: 替换为2个单引号,你还不如直接用参数方式
下载cyq.data的开源版本v4.55里面就有防sql注入函数~~~
直接参数化sql语句,何必去过滤呢
参数化查询能够过滤<div>这种样式注入吗?
@hamigua: 你这算是注入吗?你是指XSS吧,这个用编码就可以搞定了。
同意楼上的,建议使用ORM或参数化SQL,不要拼字符串,拼字符串不可能完全避免SQL注入
参数化查询能够过滤<div>这种样式注入吗?
<div>这种 要转意,不是过滤,HttpServerUtility.HtmlEncode