代码之家  ›  专栏  ›  技术社区  ›  Jamie Taylor

在ASP.Net中防止SQL注入

  •  9
  • Jamie Taylor  · 技术社区  · 14 年前

    我有这个密码

    UPDATE OPENQUERY (db,'SELECT * FROM table WHERE ref = ''"+ Ref +"'' AND bookno = ''"+ Session("number") +"'' ') 
    

    如何防止对此进行SQL注入?

    更新

    SqlCommand cmd = new SqlCommand("Select * from Table where ref=@ref", con); 
    cmd.Parameters.AddWithValue("@ref", 34);
    

    出于某种原因,我尝试添加的所有内容似乎都不起作用 SQL Command 在下面提到。

    错误在于

    'SqlCommand' is a type and cannot be used as an expression
    

    我正在接管别人的工作,所以这对我来说是全新的,我希望以正确的方式来做事情,如果有人能提供更多帮助,让我的上述查询免受SQL注入的影响,那么请这样做。

    更新2

    瓦西尔普这样说的时候我把代码加进去了

    Dim dbQuery As [String] = "SELECT * FROM table WHERE ref = '" & Tools.SQLSafeString(Ref) & "' AND bookno = '" & Tools.SQLSafeString(Session("number")) & "'"
    

    但我犯了个错误 Tools is not declared 我需要指定一个特定的名称空间吗?

    更新

    更新

    我现在有了它,所以它在没有参数的情况下工作,这是我更新的源代码,知道它为什么不添加参数值吗?

    Dim conn As SqlConnection = New SqlConnection("server='server1'; user id='w'; password='w'; database='w'; pooling='false'")
       conn.Open()
    
    
    Dim query As New SqlCommand("Select * from openquery (db, 'Select * from table where investor = @investor ') ", conn)
    query.Parameters.AddWithValue("@investor", 69836)
    
    dgBookings.DataSource = query.ExecuteReader
    dgBookings.DataBind()
    

    它是这样工作的

    Dim conn As SqlConnection = New SqlConnection("server='server1'; user id='w'; password='w'; database='w'; pooling='false'")
       conn.Open()
    
    
    Dim query As New SqlCommand("Select * from openquery (db, 'Select * from table where investor = 69836') ", conn)
    
    dgBookings.DataSource = query.ExecuteReader
    dgBookings.DataBind()
    

    我得到的错误是

    An error occurred while preparing a query for execution against OLE DB provider 'MSDASQL'. 
    

    @investor 69836

    有什么想法吗?

    解决方案

    Dim conn As SqlConnection = New SqlConnection("server='h'; user id='w'; password='w'; database='w'; pooling='false'")
    
    conn.Open()
    
    Dim query As New SqlCommand("DECLARE @investor varchar(10), @sql varchar(1000) Select @investor = 69836 select @sql = 'SELECT * FROM OPENQUERY(db,''SELECT * FROM table WHERE investor = ''''' + @investor + ''''''')' EXEC(@sql)", conn)
    
    dgBookings.DataSource = query.ExecuteReader
    dgBookings.DataBind()
    

    现在我可以编写查询而不用担心SQL注入

    8 回复  |  直到 9 年前
        1
  •  19
  •   Community pid    7 年前

    尝试使用 parameterized query 这里有一个链接 http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/

    另外,不要使用OpenQuery。。。使用this运行select

    SELECT * FROM db...table WHERE ref = @ref AND bookno = @bookno
    

    更多文章描述了您的一些选择:

    http://support.microsoft.com/kb/314520

    What is the T-SQL syntax to connect to another SQL Server?


    编辑

    注意:您最初的问题是询问分布式查询和链接服务器。此新语句不引用分布式查询。我只能假设你现在直接连接到数据库。下面是一个应该有用的例子。 这是另一个参考站点 SqlCommand.Parameters

    SqlCommand cmd = new SqlCommand("Select * from Table where ref=@ref", con); 
    cmd.Parameters.Add("@ref", SqlDbType.Int);
    cmd.Parameters["@ref"] = 34;
    

    好的,杰米·泰勒,我会再回答你的问题。

    您正在使用OpenQuery,因为您可能正在使用链接数据库

    基本上,问题是OpenQuery方法接受一个字符串,不能将变量作为发送给OpenQuery的字符串的一部分传递给它。

        Dim conn As SqlConnection = New SqlConnection("your SQL Connection String")
        Dim cmd As SqlCommand = conn.CreateCommand()
        cmd.CommandText = "Select * db...table where investor = @investor"
        Dim parameter As SqlParameter = cmd.CreateParameter()
        parameter.DbType = SqlDbType.Int
        parameter.ParameterName = "@investor"
        parameter.Direction = ParameterDirection.Input
        parameter.Value = 34
    
        2
  •  5
  •   Will Marcouiller    14 年前

    使用参数而不是连接SQL查询。

    假设您的数据库引擎是SQL Server,这里有一段代码,希望对您有所帮助。

    Using connection As SqlConnection = new SqlConnection("connectionString")
        connection.Open()
    
        Using command As SqlCommand = connection.CreateCommand()
            string sqlStatement = "select * from table where ref = @ref and bookno = @bookno";
            command.CommandText = sqlStatement
            command.CommandType = CommandType.Text
    
            Dim refParam As SqlDataParameter = command.CreateParameter()
            refParam.Direction = ParameterDirection.Input
            refParam.Name = "@ref"
            refParam.Value = Ref
    
            Dim booknoParam As SqlDataParameter = command.CreateParameter()
            booknoParam.Direction = ParameterDirection.Input
            booknoParam.Name = "@bookno"
            booknoParam.Value = Session("number")
    
            Try
                Dim reader As SqlDataReader = command.ExecuteQuery()
                ' Do your reading job here...'
            Finally
                command.Dispose()
                connection.Dispose()
            End Try
        End Using
    End Using
    

    总之,要不惜一切代价避免SQL语句连接,并使用参数化的quesries!

    How To: Protect From SQL Injection in ASP.NET

        3
  •  4
  •   Will Marcouiller    14 年前

    使用sqlparameters,如:

    SqlCommand cmd = new SqlCommand("Select * from Table where id=@id", con);
    cmd.Parameters.AddWithValue("@id", 34);
    
        4
  •  2
  •   Brandon Frohbieter    14 年前
        5
  •  2
  •   Maksim Vi.    14 年前
    SqlCommand cmd = new SqlCommand("Select * from Table where ref=@ref", con); 
    cmd.Parameters.AddWithValue("@ref", 34);
    

    试试像这样的

    Dim cmd As New SqlCommand("Select * from Table where ref=@ref", con)
    cmd.Parameters.AddWithValue("ref", 34)
    
        6
  •  1
  •   Anthony Greco    14 年前

    我首选的方法是让Visual Studio通过创建一个DAL来处理这一切: http://www.asp.net/data-access/tutorials/creating-a-data-access-layer-cs

        7
  •  1
  •   awrigley    14 年前

    使用LINQ。它自动参数化查询。

        8
  •  1
  •   Alex    14 年前

    ORM 作为另一种选择(如果你正在建造中型或大型的建筑,这是一个很好的方法)。配置它需要一点时间,但是开发变得非常快。你从本地人中选择, Linq to SQL Entity Framework any other 与.NET一起工作的ORM。