最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2009年12月24日 01:24:49   作者:  
關(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ù)進(jìn)行處理
(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("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下載

相關(guān)文章

  • MVC框架是什么 這里為你解答

    MVC框架是什么 這里為你解答

    MVC是一個設(shè)計模式,它強制性的使應(yīng)用程序的輸入、處理和輸出分開。這篇文章為大家詳細(xì)介紹了MVC框架是什么,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • asp.net字符串處理類代碼

    asp.net字符串處理類代碼

    asp.net字符串處理類代碼,需要的朋友可以參考下
    2012-06-06
  • Asp.Net Core利用文件監(jiān)視進(jìn)行快速測試開發(fā)詳解

    Asp.Net Core利用文件監(jiān)視進(jìn)行快速測試開發(fā)詳解

    這篇文章主要給大家介紹了關(guān)于Asp.Net Core利用文件監(jiān)視進(jìn)行快速測試開發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • asp.net實現(xiàn)多個文件同時下載功能

    asp.net實現(xiàn)多個文件同時下載功能

    這篇文章主要為大家詳細(xì)介紹了asp.net實現(xiàn)多個文件同時下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • ASP.NET中驗證控件的使用方法

    ASP.NET中驗證控件的使用方法

    這篇文章主要內(nèi)容是ASP.NET中驗證控件的使用方法,RequiredFieldValidation控件的介紹,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-08-08
  • Asp.net Core Jenkins Docker實現(xiàn)一鍵化部署的實現(xiàn)

    Asp.net Core Jenkins Docker實現(xiàn)一鍵化部署的實現(xiàn)

    這篇文章主要介紹了Asp.net Core Jenkins Docker實現(xiàn)一鍵化部署的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • asp.net Repeater之非常好的數(shù)據(jù)分頁

    asp.net Repeater之非常好的數(shù)據(jù)分頁

    asp.net Repeater之非常好的數(shù)據(jù)分頁實現(xiàn)代碼。
    2009-07-07
  • .net前臺調(diào)用后臺函數(shù)的簡單實例

    .net前臺調(diào)用后臺函數(shù)的簡單實例

    這篇文章介紹了.net前臺調(diào)用后臺函數(shù)的簡單實例,有需要的朋友可以參考一下
    2013-09-09
  • .net基礎(chǔ)收集匯總

    .net基礎(chǔ)收集匯總

    最近的面試讓我知道基礎(chǔ)知識的重要性,而我也每天都在網(wǎng)上找一些基礎(chǔ)題來看。其實面試無非都是一些理論基礎(chǔ),只有基礎(chǔ)過關(guān)了,才會被問到技術(shù)性的問題,所以第一關(guān)一定要打好
    2013-07-07
  • 顯示非站點目錄及映射網(wǎng)絡(luò)磁盤路徑的圖片

    顯示非站點目錄及映射網(wǎng)絡(luò)磁盤路徑的圖片

    本文就將教你怎樣顯示非站點目錄下的圖片,你可以顯示站點所在服務(wù)器所有驅(qū)動器目錄的圖片,以及映射網(wǎng)絡(luò)磁盤路徑的圖片,感興趣的朋友可以了解下就當(dāng)鞏固知識了或許對你學(xué)習(xí).net有所幫助
    2013-02-02

最新評論

安庆市| 台中市| 泰兴市| 万山特区| 石城县| 泽库县| 车险| 海盐县| 彝良县| 西宁市| 平顺县| 桃江县| 阿图什市| 桑植县| 宾川县| 墨江| 上杭县| 连江县| 桐柏县| 本溪市| 彭山县| 秦安县| 定襄县| 平潭县| 凤城市| 永靖县| 三门峡市| 九江县| 肥东县| 商都县| 武隆县| 博白县| 湘西| 石河子市| 深泽县| 辛集市| 乡宁县| 西安市| 随州市| 喀喇| 宿迁市|