Three methods for preventing Database SQL Injection in asp.net

Source: Internet
Author: User

I. One method
/// <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 static bool SqlFilter2 (string InText)
{
String word = "and | exec | insert | select | delete | update | chr | mid | master | or | truncate | char | declare | join | '";
If (InText = null)
Return false;
Foreach (string str_t in word. Split ('| '))
{
If (InText. toLower (). indexOf (str_t + "")>-1) | (InText. toLower (). indexOf ("" + str_t)>-1) | (InText. toLower (). indexOf (str_t)>-1 ))
{
Return true;
}
}
Return false;
}

2. A method in global. ascx
/// <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 str_t in this. Request. Form)
{
If (str_t = "_ VIEWSTATE") continue;
This. goErr (this. Request. Form [str_t]. ToString ());
}
// Traverse Get parameters.
Foreach (string str_t in this. Request. QueryString)
{
This. goErr (this. Request. QueryString [str_t]. ToString ());
}
}

3. Another method in global. asax

/// <Summary>
/// Check whether the parameter contains SQL characters
/// </Summary>
/// <Param name = "tm"> </param>
Private void goErr (string tm)
{
If (wenlong. function. func. SqlFilter2 (tm ))
This. Response. Redirect ("/error ");
// Response. Redirect ();
}

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.