zoukankan      html  css  js  c++  java
  • ASP.NET 4学习笔记(1) SQL注入攻击及解决方案.

     一, 定义:所谓SQL注入攻击是应用程序开发人员未预期地把SQL代码传入到应用程序的过程,只有那些直接使用用户提供的值构造SQL语句的应用程序才会受影响.

      例如原SQL代码为:

      select Orders.CustomerID,Orders.OrderID,Count(UnitPrice) as Items,SUM(UnitPrice*Quantity) as Total from Orders INNER JOIN [Order Details]on Orders.OrderID=[Order Details].OrderID where Orders.CustomerID='"+txtId.Text+"' GROUP BY Orders.OrderID,Orders.CustomerID

      如果在txtId.Text所在的文本框中输入字符串:ALFKI' or '1'='1将会返回所有的订单记录,即便那些订单不是由ALFKI创建的,因为对每一行而言,1=1总是为true.

    解决方案:采用参数化命令:

      如使用参数化命令重写前面的代码为:

     protected void btnQuery_Click(object sender, EventArgs e)
    {
    string conStr = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
    SqlConnection con = new SqlConnection(conStr);
    con.Open();
    string strSql = "select Orders.CustomerID,Orders.OrderID,Count(UnitPrice) as Items,SUM(UnitPrice*Quantity) as Total from Orders INNER JOIN [Order Details]on Orders.OrderID=[Order Details].OrderID where Orders.CustomerID=@CustomerID GROUP BY Orders.OrderID,Orders.CustomerID";
    SqlCommand cmd = new SqlCommand(strSql, con);
    cmd.Parameters.AddWithValue("@CustomerID", txtId.Text.Trim().ToString());
    SqlDataReader reader = cmd.ExecuteReader();
    GridView1.DataSource = reader;
    GridView1.DataBind();
    reader.Close();
    con.Close();
    }

      这样就可以避免SQL注入攻击.

      

  • 相关阅读:
    csrf漏洞
    WebServer远程部署
    URL跳转与钓鱼
    代码注入
    暴跌之后-如何低位灵活补仓
    操盘策略:在交易之前做好应变准备
    操盘策略:股价异动未必主力所为
    赖在长沙的50个理由
    倒在黎明前:融资客股市震荡中被强*损失850万
    操盘策略:巧用盘中T+0交易
  • 原文地址:https://www.cnblogs.com/mcgrady/p/2225735.html
Copyright © 2011-2022 走看看