ASP. NET solution to prevent SQL Injection

Source: Internet
Author: User

 

 

 

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.

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.