过滤网址和输入框中的特殊字符,防止sql注入
2024-08-30 01:50:39
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- /// <summary>
- ///cedar 的摘要说明
- /// </summary>
- public class cedar:IHttpModule
- {
- public cedar()
- {
- //
- //TODO: 在此处添加构造函数逻辑
- //
- }
- public void Dispose()
- {
- }
- public void Init(HttpApplication application)
- {
- application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
- }
- private void application_AcquireRequestState(object sender, EventArgs e)
- {
- HttpContext content = ((HttpApplication)sender).Context;
- try
- {
- string sqlErrorPage = "default.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("您访问的页面发生错误,此问题我们已经记录并尽快改善,请稍后再试。<br><a href=""+sqlErrorPage+"" mce_href=""+sqlErrorPage+"">转到首页</a>");
- 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("您访问的页面发生错误,此问题我们已经记录并尽快改善,请稍后再试。");
- 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";
- 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;
- }
- }
在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>
最新文章
- O(1)效率的表面模糊算法优化。
- jq仿淘宝放大镜插件
- vim基本命令之剪切复制粘贴替换
- 修改PHP上传文件大小限制的方法
- 最大子矩阵和 URAL 1146 Maximum Sum
- BZOJ4012 [HNOI2015]开店
- Android -- 资源使用和总结经验分享
- Vagrant搭建Ubuntu-JavaEE开发环境——Tomcat+JDK+MySQL+dubbo+测试
- 项目开发中常用到的SQL语句
- Oracle静默安装-简单记录
- CSDN 高校俱乐部: 排列搜索
- python实现邮件发送完整代码(带附件发送方式)
- javascript-变量-作用域
- H5 Canvas图像模糊解决办法
- 10分钟 5步 发布以太坊 ERC20 代币
- Nessus中文报告自动化脚本
- 1.1.19 Word中表格自动断开
- [leetcode]205. Isomorphic Strings 同构字符串
- .NET设计模式 第二部分 创建型模式(1)—:单件模式(Singleton Pattern)
- Android 面试题(答案最全)