As long as you do the following, the website will be safer and easy to maintain.
I. Data Verification
Copy codeThe Code is as follows: 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]
+ $ ");
}
}
Ii. Web. config
Add a tag in your Web. config file as follows:Copy codeThe Code is as follows: <etettings>
<Add key = "safeParameters" value = "OrderID-int32, mermeremail-email, ShippingZipcode-
USzip "/>
</AppSettings>
Key is followed by a value such as "OrderId-int32", where "-" represents the name of the parameter such as: OrderId, followed by int32 represents the data type.
Iii. Global. asax
Add the following section to Global. asax:Copy codeThe Code is as follows: protected void Application_BeginRequest (Object sender, EventArgs e ){
String [] safeParameters = System. Configuration. ConfigurationSettings. deleettings
["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 ");
}
}
You can modify only the three files later, and the maintenance efficiency of the entire system will be improved, of course, you can also add other variable parameters and data types as needed.