To prevent SQL injection, modifying files one by one is not only troublesome but also dangerous. Next I will explain how to prevent injection from the entire system.
In the following three steps, we believe that your program will be safer and the maintenance of the entire website will be simplified.
I. Data Verification
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:
<Deleetask>
<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:
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 ");
}
}
In the future, we only need to modify the above three files. The maintenance of the entire system will greatly improve the efficiency, of course, you can add other variable parameters and data types as needed.