欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net 防止SQL注入攻擊

 更新時間:2009年06月05日 00:51:34   作者:  
asp.net網(wǎng)站防止SQL注入攻擊,通常的辦法是每個文件都修改加入過濾代碼,這樣很麻煩,下面介紹一種辦法,可以從整個網(wǎng)站防止注入。
只要做到以下三點,網(wǎng)站就會比較安全了而且維護也簡單。
一、數(shù)據(jù)驗證類
復制代碼 代碼如下:

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文件中,在下面增加一個標簽,如下:
復制代碼 代碼如下:

<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");
}
}

以后需要修改的時候大家只修改以上三個文件就可以了,整個系統(tǒng)的維護效率將會提高,當然你也可以根據(jù)自己的需要增加其它的變量參數(shù)和數(shù)據(jù)類型等等。

相關文章

最新評論