SQL Injection in .NET

后端 未结 5 1494
自闭症患者
自闭症患者 2021-01-23 17:01

Hi I was wondering if anyone knew of some good websites detailing prevention for SQL injection for .NET web applications. Any resources would be greatly appricated, thank you.

相关标签:
5条回答
  • 2021-01-23 17:39

    The MSDN Magazine article Stop SQL Injection Attacks Before They Stop You seems to be fairly complete.

    While containing less detailed information about your specific question, SDL Embraces The Web is a good source of other things you should be thinking about in addition to preventing SQL injection attacks.

    The usual disclaimers apply, I don't necessarily agree with all of the information presented in those articles, but the information presented will hopefully get you thinking about ways SQL injection (and other) attacks can be mitigated on a public website.

    0 讨论(0)
  • 2021-01-23 17:47

    the first thing to know is to parameterize your queries or use stored procs....

    Never use ad-hoc sql in code where you just append the value

    give only read and write permissions (or only read for those pages that should not write)

    0 讨论(0)
  • 2021-01-23 17:51

    If you use the SqlCommand.Parameters collection to pass parameters and never inject user text into you Sql query text, there's no risk.

    0 讨论(0)
  • 2021-01-23 17:55

    I think that, if you google a bit on 'preventing sql injection in .NET', you'll find lots of good resources. :)

    Anyway, one very important thing, is to not use string-concatenation in order to build your queries. Instead, use parametrized queries. ADO.NET allows to do this, in a very easy way:

    string sql = "SELECT * FROM Persons WHERE Persons.Lastname LIKE @p_Name";
    
    SqlCommand cmd = new SqlCommand (sql);
    
    cmd.Parameters.Add ("@p_Name", SqlDbType.Varchar).Value = textBox1.Text + "%";
    
    0 讨论(0)
  • 2021-01-23 18:04
    • golden rule: never concatenate user input
    • if you write your own command strings in .NET, use the Parameters collection
    • if you use LINQ, it will usually do it for you
    • if you write commands in TSQL, use sp_executesql or your vendor's equivalent
    0 讨论(0)
提交回复
热议问题