SQL Injection prevention

Source: Internet
Author: User
Tags sql injection attack sql injection prevention

Add this section to the file global. asax.
Void application_beginrequest (Object source, eventargs E)
{

Comp. processrequest Pr = new Comp. processrequest ();
Pr. startprocessrequest ();
}

Create a C # class file named processrequest. CS.
Then copy the entire file below.

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Namespace comp
{
Public class processrequest
{
Public void startprocessrequest ()
{
Try
{
String getkeys = "";
String sqlerrorpage = "/";
If (system. Web. httpcontext. Current. Request. querystring! = NULL)
{

For (INT I = 0; I <system. Web. httpcontext. Current. Request. querystring. Count; I ++)
{
Getkeys = system. Web. httpcontext. Current. Request. querystring. Keys [I];
If (! Processsqlstr (system. Web. httpcontext. Current. Request. querystring [getkeys]. tolower ()))
{
System. Web. httpcontext. Current. response. Redirect (sqlerrorpage );
System. Web. httpcontext. Current. response. End ();
}
}
}
}
Catch
{

}
}
/**/
/// <Summary>
/// Analyze whether the user request is normal
/// </Summary>
/// <Param name = "str"> input user to submit data </param>
/// <Returns> whether SQL injection attack code is returned </returns>
Private bool processsqlstr (string Str)
{
Bool returnvalue = true;
Try
{
If (STR! = "" & Str! = NULL)
{
String sqlstr = "";
If (sqlstr = "" | sqlstr = NULL)
{
Sqlstr = "'| and | exec | insert | select | Delete | update | count | * | CHR | mid | master | truncate | char | declare ";
}
String [] anysqlstr = sqlstr. Split ('| ');
Foreach (string SS in anysqlstr)
{
If (Str. indexof (SS)> = 0)
{
Returnvalue = false;
}
}
}
}
Catch
{
Returnvalue = false;
}
Return returnvalue;
}
}
}

 

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.