asp.net 防止SQL注入攻擊
更新時(shí)間:2009年06月05日 00:51:34 作者:
asp.net網(wǎng)站防止SQL注入攻擊,通常的辦法是每個(gè)文件都修改加入過(guò)濾代碼,這樣很麻煩,下面介紹一種辦法,可以從整個(gè)網(wǎng)站防止注入。
只要做到以下三點(diǎn),網(wǎng)站就會(huì)比較安全了而且維護(hù)也簡(jiǎn)單。
一、數(shù)據(jù)驗(yàn)證類
parameterCheck.cs
public class parameterCheck{
public static bool isEmail(string emailString){
return System.Text.RegularExpressions.Regex.IsMatch(emailString, "['\\w_-]+(\\.
['\\w_-]+)*@['\\w_-]+(\\.['\\w_-]+)*\\.[a-zA-Z]{2,4}");
}
public static bool isInt(string intString){
return System.Text.RegularExpressions.Regex.IsMatch(intString ,"^(\\d{5}-\\d{4})|
(\\d{5})$");
}
public static bool isUSZip(string zipString){
return System.Text.RegularExpressions.Regex.IsMatch(zipString ,"^-[0-9]+$|^[0-9]
+$");
}
}
二、Web.config
在你的Web.config文件中,在下面增加一個(gè)標(biāo)簽,如下:
<appSettings>
<add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-
USzip" />
</appSettings>
其中key是后面的值為“OrderId-int32”等,其中“-”前面表示參數(shù)的名稱比如:OrderId,后面的int32表示數(shù)據(jù)類型。
三、Global.asax
在Global.asax中增加下面一段:
protected void Application_BeginRequest(Object sender, EventArgs e){
String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings
["safeParameters"].ToString().Split(',');
for(int i= 0 ;i < safeParameters.Length; i++){
String parameterName = safeParameters[i].Split('-')[0];
String parameterType = safeParameters[i].Split('-')[1];
isValidParameter(parameterName, parameterType);
}
}
public void isValidParameter(string parameterName, string parameterType){
string parameterValue = Request.QueryString[parameterName];
if(parameterValue == null) return;
if(parameterType.Equals("int32")){
if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");
}
else if (parameterType.Equals("double")){
if(!parameterCheck.isDouble(parameterValue)) Response.Redirect("parameterError.aspx");
}
else if (parameterType.Equals("USzip")){
if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");
}
else if (parameterType.Equals("email")){
if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");
}
}
以后需要修改的時(shí)候大家只修改以上三個(gè)文件就可以了,整個(gè)系統(tǒng)的維護(hù)效率將會(huì)提高,當(dāng)然你也可以根據(jù)自己的需要增加其它的變量參數(shù)和數(shù)據(jù)類型等等。
一、數(shù)據(jù)驗(yàn)證類
復(fù)制代碼 代碼如下:
parameterCheck.cs
public class parameterCheck{
public static bool isEmail(string emailString){
return System.Text.RegularExpressions.Regex.IsMatch(emailString, "['\\w_-]+(\\.
['\\w_-]+)*@['\\w_-]+(\\.['\\w_-]+)*\\.[a-zA-Z]{2,4}");
}
public static bool isInt(string intString){
return System.Text.RegularExpressions.Regex.IsMatch(intString ,"^(\\d{5}-\\d{4})|
(\\d{5})$");
}
public static bool isUSZip(string zipString){
return System.Text.RegularExpressions.Regex.IsMatch(zipString ,"^-[0-9]+$|^[0-9]
+$");
}
}
二、Web.config
在你的Web.config文件中,在下面增加一個(gè)標(biāo)簽,如下:
復(fù)制代碼 代碼如下:
<appSettings>
<add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-
USzip" />
</appSettings>
其中key是后面的值為“OrderId-int32”等,其中“-”前面表示參數(shù)的名稱比如:OrderId,后面的int32表示數(shù)據(jù)類型。
三、Global.asax
在Global.asax中增加下面一段:
復(fù)制代碼 代碼如下:
protected void Application_BeginRequest(Object sender, EventArgs e){
String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings
["safeParameters"].ToString().Split(',');
for(int i= 0 ;i < safeParameters.Length; i++){
String parameterName = safeParameters[i].Split('-')[0];
String parameterType = safeParameters[i].Split('-')[1];
isValidParameter(parameterName, parameterType);
}
}
public void isValidParameter(string parameterName, string parameterType){
string parameterValue = Request.QueryString[parameterName];
if(parameterValue == null) return;
if(parameterType.Equals("int32")){
if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");
}
else if (parameterType.Equals("double")){
if(!parameterCheck.isDouble(parameterValue)) Response.Redirect("parameterError.aspx");
}
else if (parameterType.Equals("USzip")){
if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");
}
else if (parameterType.Equals("email")){
if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");
}
}
以后需要修改的時(shí)候大家只修改以上三個(gè)文件就可以了,整個(gè)系統(tǒng)的維護(hù)效率將會(huì)提高,當(dāng)然你也可以根據(jù)自己的需要增加其它的變量參數(shù)和數(shù)據(jù)類型等等。
相關(guān)文章
利用IIS調(diào)試ASP.NET網(wǎng)站程序的完整步驟
這篇文章主要給大家介紹了關(guān)于利用IIS調(diào)試ASP.NET網(wǎng)站程序的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11ASP.NET session.timeout設(shè)置案例詳解
這篇文章主要介紹了ASP.NET session.timeout設(shè)置案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08ASP.NET數(shù)據(jù)庫(kù)存取圖片的方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET數(shù)據(jù)庫(kù)如何存取圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01ASP.Net Core基于ABP架構(gòu)配置To Json序列化
這篇文章介紹了ASP.Net Core基于ABP架構(gòu)配置To Json序列化的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06asp.net(C#)把漢字轉(zhuǎn)化成全拼音函數(shù)(全拼)
asp.net(C#)把漢字轉(zhuǎn)化成全拼音函數(shù)的代碼,需要的朋友可以參考下。2009-12-12ASP.NET中基于soaphead的webservice安全機(jī)制
常會(huì)用到WebService來(lái)通訊,但WebService發(fā)布后為了能調(diào)用,一般都通過(guò)發(fā)布到IIS后調(diào)用 的。在IIS里可以通過(guò)匿名訪問(wèn),但這樣大家都可能訪問(wèn),不安全,下面提供一種基于soaphead的安全機(jī)制。2016-05-05.NET?Framework?的項(xiàng)目如何使用?FTP?下載文件
本文專門針對(duì)面向?.NET?Framework?的項(xiàng)目,?對(duì)于面向?.NET?6?及更高版本的項(xiàng)目,不再支持?FTP,此示例演示如何從?FTP?服務(wù)器下載文件,感興趣的朋友跟隨小編一起看看吧2024-01-01