ASP. NET prevents SQL InjectionGenerally, 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
The following is a code snippet: 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 }) Activities $ "); } Public static bool isuszip (string zipstring ){ Return System. Text. regularexpressions. RegEx. ismatch (zipstring, "^-[0-9] + keys $ | ^ [0-9] + keys $ "); } } |
Ii. Web. config In your web. config fileAdd a label as follows:
The following is a code snippet: <Deleetask> <Add key = "safeparameters" value = "OrderID-int32, mermeremail-email, shippingzipcode-uszip"/> </Appsettings> |
Where key is The value is "OrderId-int32" and so on, 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:
The following is a code snippet: 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. Split ('-') [0]; String parametertype = safeparameters. 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. |