激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - asp.net利用HttpModule實現(xiàn)防sql注入

asp.net利用HttpModule實現(xiàn)防sql注入

2019-06-29 16:12服務(wù)器之家 ASP.NET教程

關(guān)于sql注入,已經(jīng)被很多人討論過了。這篇沒有新意功能也不夠通用,nnd,不想引起口水,就是覺得簡單而且思路有參考性才貼出來。

1、新建一個類,實現(xiàn)IHttpModule接口 
代碼 
復(fù)制代碼代碼如下:

public class SqlHttpModule : IHttpModule 

public void Dispose() 


public void Init(HttpApplication context) 

context.AcquireRequestState += new EventHandler(context_AcquireRequestState); 


在實現(xiàn)接口的Init方法時,我們選擇了AcquireRequestState事件,為什么不是Begin_Request事件呢?這是因為我們在處理的時候可能用的session,而Begin_Request事件執(zhí)行的時候還沒有加載session狀態(tài)(關(guān)于HttpModule可以參考這一篇)。 
2、對網(wǎng)站提交的數(shù)據(jù)進行處理 
(1)、GET方式 
代碼 
復(fù)制代碼代碼如下:

//url提交數(shù)據(jù) 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方式 
代碼 
復(fù)制代碼代碼如下:

//表單提交數(shù)據(jù) 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!"); 



完整代碼: 
代碼 
復(fù)制代碼代碼如下:

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提交數(shù)據(jù) 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!"); 



//表單提交數(shù)據(jù) 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> 
/// 過濾非法關(guān)鍵字,這個可以按照項目靈活配置 
/// </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項目中應(yīng)用 
只要在web.config的httpModules節(jié)點下面添加如下配置即可。 
<httpModules> 
<add name="SqlHttpModule" type="DotNet.Common.WebForm.SqlHttpModule, DotNet.Common.WebForm"></add> 
</httpModules> 
需要說明的是,這個防止sql注入的方法在特定的小項目中還是很簡潔高效的,但是不通用,通常我們都是選擇參數(shù)化(利用orm或者ado.net的參數(shù)化)方式防止sql注入。 
附:asp.net在網(wǎng)頁頭部引入js腳本的簡單方法 
asp.net開發(fā)少不了JavaScript的輔助。在通常項目中,js文件都組織在一個公共目錄如js文件夾下。隨著項目的深入,你會發(fā)現(xiàn)js腳本文件越來越多,公共的腳步庫越來越龐大。實際使用的時候,我們通常都是在頁面中通過 <script src="..." type="text/javascript" >形式引入js文件,而且引入的越來越多。下面我們就來簡單討論在每個頁面引入公共腳本庫的統(tǒng)一方式,而不用每個頁面都是很多<script src="..." type="text/javascript" >的形式。 
和我們以前的做法一樣,定義一個頁面基類叫BasePage,事件和方法如下: 
Code 
復(fù)制代碼代碼如下:

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();//向網(wǎng)頁頭部添加js等文件 

#region 網(wǎng)頁頭添加通用統(tǒng)一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 


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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久久国 | 激情久久一区二区 | 日本视频免费 | 欧美xxxxx视频 | 国产毛片aaa一区二区三区视频 | 久久另类视频 | 伊人成人免费视频 | 精品国产一区二区久久 | 欧美成人免费tv在线播放 | 九九精品在线播放 | 久久人人爽人人爽人人片av免费 | 蜜桃精品视频 | 蜜桃一本色道久久综合亚洲精品冫 | 国产精品久久久久久影视 | 日本黄色免费播放 | 国产一区二区免费在线观看 | 成人一区二区在线观看视频 | 日本成人一区二区 | 他也色在线视频 | 九九色精品 | 欧美综合成人 | 精品国产一区二区在线观看 | 国产精品久久久久久久久久三级 | 成人免费淫片视频软件 | 91网站免费观看 | 成人激情视频网 | 国产99视频在线观看 | 国产精品久久久久影院老司 | 成人三级视频网站 | 99精品国产一区二区三区 | 91精品久久久久久久久久久 | 久草在线视频新 | 色婷婷久久久亚洲一区二区三区 | 成人在线观看免费 | 一级黄色毛片免费 | 精品国产99久久久久久宅男i | 精品影视一区二区 | 国产宾馆3p国语对白 | 久久久久久久九九九九 | 视频一区二区三区在线播放 | 久久久久性 |