Asp.net anti-SQL Injection parameter Filtering

Source: Internet
Author: User
// <Summary>
// Trigger an event when data is handed in
/// </Summary>
// <Param name = "sender"> </param>
// <Param name = "e"> </param>
Protected void Application_BeginRequest (Object sender, EventArgs e)
{
// Traverse the Post parameter, except for hidden fields
Foreach (string I in this. Request. Form)
{
If (I = "_ VIEWSTATE") continue;
This. goErr (this. Request. Form [I]. ToString ());
}
// Traverse Get parameters.
Foreach (string I in this. Request. QueryString)
{
This. goErr (this. Request. QueryString [I]. ToString ());

}
}

/// <Summary>
/// SQL Injection Filtering
/// </Summary>
/// <Param name = "InText"> string to be filtered </param>
/// <Returns> If the parameter contains insecure characters, true is returned. </returns>
Public bool SqlFilter (string InText)
{
String word = "and | exec | insert | select | delete | update | chr | mid | master | or | truncate | char | declare | join | cmd ";
If (InText = null)
Return false;
Foreach (string I in word. Split ('| '))
{
If (InText. toLower (). indexOf (I + "")>-1) | (InText. toLower (). indexOf ("" + I)>-1 ))
{
Return true;
}
}
Return false;
}

/// <Summary>
/// Check whether the parameter contains SQL characters
/// </Summary>
/// <Param name = "tm"> </param>
Private void goErr (string tm)
{
If (SqlFilter (tm ))
{
Response. Write ("<script> window. alert ('the data you entered has an incorrect parameter! '); "+" </"+" Script> ");
}
}

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.