Asp.net prevents SQL injection attacks

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.