Simple Asp.net anti-SQL Injection

Source: Internet
Author: User

Preventing SQL injection is a problem that every developer must take an exam.

Asp.net has a global. asax file and has an application_beginrequest method (obtained by the application startup)

This is the event triggered when the parameters are obtained. This is the event that the website page will go through every time it is submitted. Here, anti-injection is implemented, and the entry is suddenly stuck.

Code As follows:
Protected void application_beginrequest (Object sender, eventargs E)
{
// Traverse the post parameter, except for hidden fields
Foreach (string I in this. Request. Form)
{
// Output
}
// Traverse get parameters.
Foreach (string I in this. Request. querystring)
{
// Output
}
}
In this way, all the parameters submitted on the page can be traversed. Only one "// output" is written, and no code is written. You can write repsonse. write output to see and try it. We only need to filter these parameters. If there are insecure characters, you can directly jump to it;
You can also add filters.
Public static string delsqlstr (string Str)
{
If (STR = NULL | STR = "")
Return "";
STR = Str. Replace (";","");
STR = Str. Replace ("'","");
STR = Str. Replace ("&","");
STR = Str. Replace ("% 20 ","");
STR = Str. Replace ("--","");
STR = Str. Replace ("= ","");
STR = Str. Replace ("<","");
STR = Str. Replace ("> ","");
STR = Str. Replace ("% ","");
Return STR;
}

 

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.