.Net程序防止被注入代碼(整站通用)分享
做到以下三步,相信的程序?qū)?huì)比較安全了,而且對(duì)整個(gè)網(wǎng)站的維護(hù)也將會(huì)變的簡(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文件中,在<appSettings>下面增加一個(gè)標(biāo)簽:如下
<appSettings>
<add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />
</appSettings>
其中key是<saveParameters>后面的值為"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è)文件,對(duì)整個(gè)系統(tǒng)的維護(hù)將會(huì)大大提高效率,當(dāng)然你可以根據(jù)自己的需要增加其它的變量參數(shù)和數(shù)據(jù)類型。
相關(guān)文章
ASP.NET?Core?MVC控制器請(qǐng)求依賴注入
這篇文章介紹了ASP.NET?Core?MVC控制器請(qǐng)求依賴注入的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04獲取遠(yuǎn)程網(wǎng)頁(yè)的內(nèi)容之二(downmoon原創(chuàng))
獲取遠(yuǎn)程網(wǎng)頁(yè)的內(nèi)容之二(downmoon原創(chuàng))...2007-03-03ASP.NET第一次訪問慢的完美解決方案(MVC,Web Api)
這篇文章主要給大家介紹了關(guān)于ASP.NET第一次訪問慢的完美解決方案(MVC,Web Api)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07.NET中RDLC循環(huán)處理數(shù)據(jù)的應(yīng)用分析
本篇文章介紹了,.NET中RDLC循環(huán)處理數(shù)據(jù)的應(yīng)用分析。需要的朋友參考下2013-05-05Asp.net mvc 權(quán)限過濾和單點(diǎn)登錄(禁止重復(fù)登錄)
這篇文章主要介紹了Asp.net mvc 權(quán)限過濾和單點(diǎn)登錄(禁止重復(fù)登錄)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12asp.net 獲取系統(tǒng)中參數(shù)的實(shí)現(xiàn)代碼
asp.net 獲取系統(tǒng)中參數(shù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12ASP.NET筆記之頁(yè)面跳轉(zhuǎn)、調(diào)試、form表單、viewstate、cookie的使用說明
ASP.NET筆記之頁(yè)面跳轉(zhuǎn)、調(diào)試、form表單、viewstate、cookie的使用說明2013-04-04