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

View: 23797|Reply: 2

[Interface] c# and asp.net prevent SQL injection filtering methods

[Copy link]
Posted on 4/19/2015 11:54:16 PM | | | |


I use asp.net for presentations, hehe

Front Desk Code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sqltest.aspx.cs" Inherits="TestWeb.sqltest" %>

  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="txtsql" runat="server"></asp:TextBox>
  11.         <asp:Button ID="btnsql" runat="server" Text="注入" onclick="btnsql_Click" />
  12.     </div>
  13.     </form>
  14. </body>
  15. </html>
Copy code
Background 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 sqltest : System.Web.UI.Page
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {

  13.         }

  14.         protected void btnsql_Click(object sender, EventArgs e)
  15.         {
  16.             
  17.             if (zhuru.ProcessSqlStr(txtsql.Text))
  18.             {
  19.                 Response.Write("安全");
  20.             }
  21.             else {
  22.                 Response.Write("危险");
  23.             }
  24.         }
  25.     }
  26. }
Copy code
zhuru.cs Class 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 zhuru
  9.     {
  10.         
  11.         public static bool ProcessSqlStr(string inputString)
  12.         {
  13.             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";
  14.             try
  15.             {
  16.                 if ((inputString != null) && (inputString != String.Empty))
  17.                 {
  18.                     string str_Regex = @"\b(" + SqlStr + @")\b";

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

  23.                 }
  24.             }
  25.             catch
  26.             {
  27.                 return false;
  28.             }
  29.             return true;
  30.         }

  31.     }
  32. }
Copy code






Previous:C#. .NET to prevent SQL injection attacks
Next:How to check the hostname of a known LAN IP address
 Landlord| Posted on 2/7/2022 7:33:01 PM |
Java dynamically spliced SQL statements prevent database injection
https://www.itsvse.com/thread-10249-1-1.html
Posted on 3/6/2022 10:40:43 AM |
Learn               
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