Asp.net anti-injection code

Source: Internet
Author: User
Tags sql injection attack
/// <Summary> /// Add the startprocessrequest () function to application_beginrequest () /// </Summary> Protected void application_beginrequest (Object sender, eventargs e) {startprocessrequest (); } # Region SQL injection attacksCodeAnalysis / // <Summary> /// Process user-submitted requests /// </Summary> Private void startprocessrequest () { Try { String getkeys = ""; String sqlerrorpage = "/default. aspx"; // If an invalid parameter exists, go to the error prompt page. 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; 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 (INT I = 0; I <system. Web. httpcontext. Current. Request. Form. Count; I ++) { Getkeys = system. Web. httpcontext. Current. Request. Form. Keys; 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 (); } } } If (system. Web. httpcontext. Current. Request. Cookies! = NULL) { For (INT I = 0; I <system. Web. httpcontext. Current. Request. Cookies. Count; I ++) { Getkeys = system. Web. httpcontext. Current. Request. Cookies. Keys; If (! Processsqlstr (system. Web. httpcontext. Current. Request. Cookies [getkeys]. tostring ())) { System. Web. httpcontext. Current. response. Redirect (sqlerrorpage ); System. Web. httpcontext. Current. response. End (); } } } } Catch { // Error handling: process user submitted information! } } /// <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; t Ry { If (Str. Trim ()! = "") { String sqlstr = "and | exec | insert | select | Delete | update | count | * | CHR | mid | master | truncate | char | declare "; String [] anysqlstr = sqlstr. Split ('| '); F Oreach (string SS in anysqlstr) { If (Str. tolower (). indexof (SS)> = 0) { Returnvalue = false; Break; } } } } Catch { Returnvalue = false; } Return returnvalue; } # Endregion [/PRE]
Related Article

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.