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 the program will be safer and the maintenance of the entire website will become simpler.
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
In your web. config file, add a tag under <etettings> as follows:
<Deleetask>
<Add key = "safeparameters" value = "OrderID-int32, mermeremail-email, shippingzipcode-uszip"/>
</Appsettings>
Key is the value after <saveparameters> is "OrderId-int32", where "-" indicates the name of the parameter, such as orderid, and int32 indicates 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.html X ");
}
Else if (parametertype. Equals ("double ")){
If (! Parametercheck. isdouble (parametervalue) response. Redirect ("parametererror.html X ");
}
Else if (parametertype. Equals ("uszip ")){
If (! Parametercheck. isuszip (parametervalue) response. Redirect ("parametererror.html X ");
}
Else if (parametertype. Equals ("email ")){
If (! Parametercheck. isemail (parametervalue) response. Redirect ("parametererror.html X ");
}
}
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.