. Net Program prevents SQL injection of source code

Source: Internet
Author: User
Tags sql injection attack
One. net Program To prevent SQL injection, use the following method: Code Add to the Global. asax file: ///<Summary> /// Prevent SQL Injection ///</Summary> ///<Param> </param> ///<Param> </param> VoidApplication_beginrequest (ObjectSender,EventargsE) { Startprocessrequest (); }

# RegionSQLInjection attack code analysis

///<Summary> /// Process user-submitted requests ///</Summary> Private VoidStartprocessrequest () { Try { StringGetkeys =""; StringSqlerrorpage ="Error. aspx";// Error prompt page for redirection If(System. Web.Httpcontext. Current. Request. querystring! =Null) { For(IntI = 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]) { System. Web.Httpcontext. Current. response. Redirect (sqlerrorpage ); System. Web.Httpcontext. Current. response. End (); } } } If(System. Web.Httpcontext. Current. Request. form! =Null) { For(IntI = 0; I <system. Web.Httpcontext. Current. Request. Form. Count; I ++) { Getkeys = system. Web.Httpcontext. Current. Request. Form. Keys [I]; If(Getkeys ="_ Viewstate")Continue; If(! Processsqlstr (system. Web.Httpcontext. Current. Request. Form [getkeys]) { System. Web.Httpcontext. Current. response. Redirect (sqlerrorpage ); System. Web.Httpcontext. Current. response. End (); } } } } Catch { // Error Handling: process user submitted information! } } ///<Summary> /// Analyze whether user requests are normal ///</Summary> ///<Param> Input user to submit data </Param> ///<Returns> Returns whether the SQL injection attack code is included. </Returns> Private BoolProcesssqlstr (StringStr) { BoolReturnvalue =True; Try { If(Str. Trim ()! ="") { StringSqlstr ="And. Exec. Insert. Select. Delete. Update. Count. *. CHR. Mid. master. truncate. Char. Declare"; String[] Anysqlstr = sqlstr. Split ('.'); Foreach(StringSSInAnysqlstr) { If(Str. tolower (). indexof (SS)> = 0) { Returnvalue =False; Break; } } } } Catch { Returnvalue =False; } ReturnReturnvalue; } # Endregion Method 2: Add a class in the app_code folderThe content of sqlzr. CS is as follows: Public Class Sqlzr { PublicSqlzr () { // // Todo: Add the constructor logic here // } Public Static StringDelsqlstr (StringStr) { 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 ("%",""); STR = Str. Replace ("+",""); STR = Str. Replace ("-",""); STR = Str. Replace ("=",""); STR = Str. Replace (",",""); ReturnSTR; } }

Then Request. querystring ["ID"]Changed: Sqlzr . Delsqlstr (request. querystring ["ID"])You can.

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.