.net viewstate 免受 SQL 注入的影响吗?

发布于 2024-11-25 11:03:05 字数 51 浏览 8 评论 0原文

我使用命令名和命令参数来控制排序(字段和方向)。视图状态免受 SQL 注入的安全性如何。

I am using commandname and commandargument to control sorting (field and direction). How secure is the viewstate from SQL injection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水晶透心 2024-12-02 11:03:05

SQL注入是将用户输入的值直接放入查询中,从而允许恶意用户访问或破坏利用您的安全漏洞来攻击您的数据库。例如,您有一个要搜索的文本框,并且它们输入的值位于实际查询中。

除非您的命令参数是由用户动态输入的,否则 SQL 注入不会构成威胁。

SQL injection is where user-entered values are put directly into queries, allowing malicious users to access or damage your database by taking advantage of your security loopholes. For example, you have a textbox to search, and the value they enter goes inside the actual query.

Unless your command argument is dynamically entered by the user then SQL injection wouldn't be a threat.

待"谢繁草 2024-12-02 11:03:05

如果您使用原始 SQL,那么您可能会使用 DataTable 对象,对吧?如果您使用的是 DataTable,则可以在使用 DataView 从数据库中提取数据后对其进行排序,并将控件绑定到 DataView。这样,您就不会授予用户提交的 SQL 数据访问权限。所以,在你的代码中你会做这样的事情:

DataTable dt = GetData(); // pull data from DB with no sort specified
DataView view = dt.DefaultView;  // Get a DataView so you can sort
view.Sort = "Col1, Col2 DESC"; // assemble sort string from your command args
MyControl.DataSource = view;
MyControl.DataBind();

If you are using raw SQL, then you are likely using DataTable objects, right? If you are using a DataTable, then you can sort that data after it's pulled from the database using a DataView and bind your control to the DataView. That way, you're not giving user submitted data access to your SQL. So, in your code you'd do something like this:

DataTable dt = GetData(); // pull data from DB with no sort specified
DataView view = dt.DefaultView;  // Get a DataView so you can sort
view.Sort = "Col1, Col2 DESC"; // assemble sort string from your command args
MyControl.DataSource = view;
MyControl.DataBind();
虐人心 2024-12-02 11:03:05

如果您要将 ViewState 中的值传递到串联 SQL 中,那么可以。但是,如果您使用绑定参数(您是,不是吗? ?)那么你就不用担心了。

差:

string sql = "select * from product where name = ' + ProductNameTextBox.Text + '"

好:

string sql = "select * from product where name = @name"

using(var command = new SqlCommand(sql, connection))
{

   SqlParameter param = new SqlServerParameter("@name", SqlDbType.VarChar, 50);
   param.Value = ProductNameTextBox.Text;

   command.Parameters.Add(param);

   command.ExecuteNonQuery();
}

If you are passing values from the ViewState into concatenated SQL, then yes. However, if you're using Bind Parameters (you are, aren't you?) then you don't have to worry about it.

Bad:

string sql = "select * from product where name = ' + ProductNameTextBox.Text + '"

Good:

string sql = "select * from product where name = @name"

using(var command = new SqlCommand(sql, connection))
{

   SqlParameter param = new SqlServerParameter("@name", SqlDbType.VarChar, 50);
   param.Value = ProductNameTextBox.Text;

   command.Parameters.Add(param);

   command.ExecuteNonQuery();
}
锦欢 2024-12-02 11:03:05

这取决于“直接放入 SQL 查询”是否意味着它们是参数化查询的参数或作为文字......如果作为文字,那么答案是否定的。

It depends whether "put directly into a SQL Query" means they are parameters of a parameterized Query or as literals... if as literals then the answer is NO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文