标签:
public class SqlHttpModule : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.AcquireRequestState += new EventHandler(context_AcquireRequestState); } }
if (context.Request.QueryString != null) { for (int i = 0; i < context.Request.QueryString.Count; i++) { key = context.Request.QueryString.Keys[i]; value = context.Server.UrlDecode(context.Request.QueryString[key]); if (!FilterSql(value)) { throw new Exception("QueryString(GET) including dangerous sql key word!"); } } }
if (context.Request.Form != null) { for (int i = 0; i < context.Request.Form.Count; i++) { key = context.Request.Form.Keys[i]; if (key == "__VIEWSTATE") continue; value = context.Server.HtmlDecode(context.Request.Form[i]); if (!FilterSql(value)) { throw new Exception("Request.Form(POST) including dangerous sql key word!"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; namespace DotNet.Common.WebForm { /// <summary> /// 简单防止sql注入 /// </summary> public class SqlHttpModule : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.AcquireRequestState += new EventHandler(context_AcquireRequestState); } /// <summary> /// 处理sql注入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void context_AcquireRequestState(object sender, EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; try { string key = string.Empty; string value = string.Empty; //url提交数据 get方式 if (context.Request.QueryString != null) { for (int i = 0; i < context.Request.QueryString.Count; i++) { key = context.Request.QueryString.Keys[i]; value = context.Server.UrlDecode(context.Request.QueryString[key]); if (!FilterSql(value)) { throw new Exception("QueryString(GET) including dangerous sql key word!"); } } } //表单提交数据 post方式 if (context.Request.Form != null) { for (int i = 0; i < context.Request.Form.Count; i++) { key = context.Request.Form.Keys[i]; if (key == "__VIEWSTATE") continue; value = context.Server.HtmlDecode(context.Request.Form[i]); if (!FilterSql(value)) { throw new Exception("Request.Form(POST) including dangerous sql key word!"); } } } } catch (Exception ex) { throw ex; } } /// <summary> /// 过滤非法关键字,这个可以按照项目灵活配置 /// </summary> /// <param name="key"></param> /// <returns></returns> private bool FilterSql(string key) { bool flag = true; try { if (!string.IsNullOrEmpty(key)) { //一般配置在公共的文件中,如xml文件,txt文本等等 string sqlStr = "insert |delete |select |update |exec |varchar |drop |creat |declare |truncate |cursor |begin |open|<-- |--> "; string[] sqlStrArr = sqlStr.Split(‘|‘); foreach (string strChild in sqlStrArr) { if (key.ToUpper().IndexOf(strChild.ToUpper()) != -1) { flag = false; break; } } } } catch { flag = false; } return flag; } } }
3、在web项目中应用
using System; using System.Data; using System.Configuration; using System.Collections.Generic; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Reflection; using System.Text; using System.IO; namespace DotNet.Common.WebForm { using DotNet.Common.Model; using DotNet.Common.Util; public class BasePage : System.Web.UI.Page { public BasePage() { } protected override void OnInit(EventArgs e) { base.OnInit(e); AddHeaderJs();//向网页头部添加js等文件 } #region 网页头添加通用统一js文件 private void AddHeaderJs() { string jsPath = "~/js/"; string filePath = Server.MapPath(jsPath); Literal lit = new Literal(); StringBuilder sb = new StringBuilder(); if (!Directory.Exists(filePath)) throw new Exception("路径不存在"); List<string> listJs = new List<string>(); foreach (var item in Directory.GetFiles(filePath, "*.js", SearchOption.TopDirectoryOnly)) { listJs.Add(Path.GetFileName(item)); } foreach (var jsname in listJs) { sb.Append(ScriptInclude(jsPath + jsname)); } lit.Text = sb.ToString(); Header.Controls.AddAt(1, lit); } private string ResolveHeaderUrl(string relativeUrl) { string url = null; if (string.IsNullOrEmpty(relativeUrl)) { url = string.Empty; } else if (!relativeUrl.StartsWith("~")) { url = relativeUrl; } else { var basePath = HttpContext.Current.Request.ApplicationPath; url = basePath + relativeUrl.Substring(1); url = url.Replace("//", "/"); } return url; } private string ScriptInclude(string url) { if (string.IsNullOrEmpty(url)) throw new Exception("路径不存在"); string path = ResolveHeaderUrl(url); return string.Format(@"<script src=‘{0}‘ type=‘text/javascript‘></script>", path); } #endregion } }
asp.net利用HttpModule实现防sql注入和加载样式和JS文件
标签:
原文地址:http://www.cnblogs.com/Alenliu/p/4937797.html