asp.net - 如何在 Web 表单中进行 Html.Encode

标签 asp.net security webforms html-encode

我有一个 ASP.NET Web 窗体应用程序。有一个带有 TextBoxes 的页面,用户可以在其中输入用于查询数据库的搜索词。

我知道我需要防止 JavaScript 注入(inject)攻击。我该怎么做呢?

在 MVC 中我会使用 Html.Encode .它似乎无法在 Web 表单中被识别。

谢谢!

最佳答案

如果您使用的是 ASP.NET 4 或更高版本,则可以使用以下语法:

<%: Model.Username %>

它将对表达式进行 HTML 编码。 Scott Gu explains the benefit of this syntax:

We chose the <%: %> syntax so that it would be easy to quickly replace existing instances of <%= %> code blocks. It also enables you to easily search your code-base for <%= %> elements to find and verify any cases where you are not using HTML encoding within your application to ensure that you have the correct behavior.

关于asp.net - 如何在 Web 表单中进行 Html.Encode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4018929/

相关文章:

jQuery 处理动态添加元素上的事件

asp.net - ASP.NET MVC 条件输出的最佳实践?

c# - 如何在asp.net core web api中上传文件

c# - 事务中的 EF 更新窗体 View

c# - 访问被拒绝冒充当前用户访问网络文件夹

java - 迁移java8到java11 : import com. sun.security.auth.module.NTSystem

c - malloc 相对于 calloc 的安全/性能影响?

ruby-on-rails - 验证失败时如何保留文件字段值

mysql - ruby on rails sql 注入(inject) LIKE '%#{argument}% 易受攻击?

c# - 网址不是有效的虚拟路径