天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

asp.net利用HttpModule實現防sql注入

1、新建一個類,實現IHttpModule接口
代碼
復制代碼 代碼如下:
public class SqlHttpModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
}

在實現接口的Init方法時,我們選擇了AcquireRequestState事件,為什么不是Begin_Request事件呢?這是因為我們在處理的時候可能用的session,而Begin_Request事件執行的時候還沒有加載session狀態(關于HttpModule可以參考這一篇)。
2、對網站提交的數據進行處理
(1)、GET方式
代碼
復制代碼 代碼如下:
//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!");
}
}
}

(2)、POST方式
代碼
復制代碼 代碼如下:
//表單提交數據 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!");
}
}
}

完整代碼:
代碼
復制代碼 代碼如下:
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項目中應用
只要在web.config的httpModules節點下面添加如下配置即可。
<httpModules>
<add name="SqlHttpModule" type="DotNET.Common.WebForm.SqlHttpModule, DotNET.Common.WebForm"></add>
</httpModules>
需要說明的是,這個防止sql注入的方法在特定的小項目中還是很簡潔高效的,但是不通用,通常我們都是選擇參數化(利用orm或者ado.NET的參數化)方式防止sql注入。
附:ASP.NET在網頁頭部引入js腳本的簡單方法
ASP.NET開發少不了JavaScript的輔助。在通常項目中,js文件都組織在一個公共目錄如js文件夾下。隨著項目的深入,你會發現js腳本文件越來越多,公共的腳步庫越來越龐大。實際使用的時候,我們通常都是在頁面中通過 <script src="..." type="text/Javascript" >形式引入js文件,而且引入的越來越多。下面我們就來簡單討論在每個頁面引入公共腳本庫的統一方式,而不用每個頁面都是很多<script src="..." type="text/Javascript" >的形式。
和我們以前的做法一樣,定義一個頁面基類叫BasePage,事件和方法如下:
Code
復制代碼 代碼如下:
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("http://", "/");
}
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
}
}

這樣就簡單地解決了引入公共js的問題。同樣的原理,你也可以引入其他類型的文件,如css等。
demo下載

AspNet技術asp.net利用HttpModule實現防sql注入,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产午夜高潮熟女精品AV | 挺进绝色老师的紧窄小肉六 | AV无码国产精品午夜A片麻豆 | 国产高清美女一级毛片久久 | 囯产精品一品二区三区 | 国语自产精品一区在线视频观看 | 国产成人无码免费精品果冻传媒 | 97人人添人人澡人人澡人人澡 | 久久精品视频免费 | YELLOW视频直播在线观看高清 | 柠檬福利精品视频导航 | 久久99热狠狠色一区二区 | 国产99久久亚洲综合精品西瓜tv | 无限资源在线观看完整版免费下载 | 久久无码AV亚洲精品色午夜 | 亚洲高清中文字幕 | 黄得让人湿的片段 | 欧美一区二区三区久久综 | 第一福利在线永久视频 | 亚洲精品成人 | 国产午夜人成在线视频麻豆 | 观赏女性排尿 | www红色一片 | 免费可以看污动画软件 | 黄色软件色多多 | 两个客户一起吃我的奶 | 中文日韩亚洲欧美字幕 | 国产成人免费在线 | 成人国内精品久久久久影 | 玖玖爱在线播放 | 好姑娘BD高清在线观看免费 | 亚洲阿v天堂在线2017 | 精品国产在线手机在线 | 精品久久久久久久国产潘金莲 | 品色堂主页 | 久久综合视频网站 | 精品国产国产综合精品 | 欧洲最强rapper潮水喷视频 | 午夜影院老司机 | 青柠在线观看视频在线 | 国产人妻人伦精品熟女麻豆 |