This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18374|Reply: 0

[ASP.NET] Batch detection of user input for SQL dangerous characters

[Copy link]
Posted on 5/8/2015 12:29:34 PM | | |
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testsql.aspx.cs" Inherits="TestWeb.testsql" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2"
  11.             runat="server"></asp:TextBox><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  12.             <asp:Button ID="Button1" runat="server" Text="Button"
  13.             onclick="Button1_Click" />
  14.     </div>
  15.    
  16.     </form>
  17. </body>
  18. </html>
Copy code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. namespace TestWeb
  8. {
  9.     public partial class testsql : System.Web.UI.Page
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {

  13.         }

  14.         protected void Button1_Click(object sender, EventArgs e)
  15.         {
  16.             string[] userinput = new string[] { TextBox1.Text, TextBox2.Text, TextBox3.Text };
  17.             bool a=SQLinfo.sql(userinput);
  18.             Response.Write(a);
  19.         }
  20.     }
  21. }
Copy code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Text.RegularExpressions;

  6. namespace TestWeb
  7. {
  8.     public class SQLinfo
  9.     {
  10.         public static bool sql(string[] inputString)
  11.         {
  12.             string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
  13.             try
  14.             {
  15.                 foreach (string sqls in inputString)
  16.                 {
  17.                     if ((sqls != null) && (sqls != String.Empty))
  18.                     {
  19.                         string str_Regex = @"\b(" + SqlStr + @")\b";

  20.                         Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
  21.                         //string s = Regex.Match(inputString).Value;
  22.                         if (true == Regex.IsMatch(sqls))
  23.                             return false;

  24.                     }
  25.                 }
  26.             }
  27.             catch
  28.             {
  29.                 return false;
  30.             }
  31.             return true;
  32.         }
  33.     }
  34. }
Copy code






Previous:Three-tier architecture of ASP.NET (DAL, BLL, UI)
Next:If you already have an open DataReader associated with this Command, you must first close it.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com