ASP.NET 4 中是否可以默认打开 HTML 编码?

发布于 2024-09-13 02:02:56 字数 181 浏览 0 评论 0原文

在 ASP.NET 4 中,我们可以使用新的 <%: ... %> 运算符来输出 HTML 编码的字符串。是否可以配置 ASP.NET 4(在 web.config 中),以便 <%= ... %> 运算符也对字符串进行 HTML 编码?

In ASP.NET 4, we can use the new <%: ... %> operator to output HTML encoded strings. Is it possible to configure ASP.NET 4 (in web.config) so that the <%= ... %> operator will also HTML encode strings?

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

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

发布评论

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

评论(2

故事和酒 2024-09-20 02:02:56

不,幸运的是。

如果您这样配置,您的开发人员将养成使用 <%= ... %> 而不是编码的习惯。

如果他们随后从事不同的(正常)项目,他们最终会忘记对输出进行编码。

ASP.Net Razor 默认为 HTML编码,因为它不存在这个问题。
(没有可以切换到默认情况下不会编码的 Razor 代码)

No, fortunately.

If you configure it this way, your developers will get into the habit of using <%= ... %> and not encoding.

If they subsequently work on a different (normal) project, they will end up forgetting to encode their output.

ASP.Net Razor does default to HTML encoding, because it doesn't have this issue.
(There is no Razor code you can switch to that won't encode by default)

山田美奈子 2024-09-20 02:02:56

就我个人而言,我不喜欢这个新的 <%: 语法。我宁愿使用 <%= HttpUtility.HtmlEncode( 因为它更加明确。

除此之外,它还给人一种错误的安全感,因为它只进行 HTML 编码。例如看这个代码片段:

function myJavaScriptFunction()
{
    var message = '<%: Person.LastComment %>';
    alert(message);
}

它不会保存,因为它没有使用正确的编码 以下是安全的:

function myJavaScriptFunction()
{
    var message = '<%: AntiXss.JavaScriptEncode(Person.LastComment) %>';
    alert(message);
}

更明确。

Personally, I don't like this new <%: syntax. I rather use <%= HttpUtility.HtmlEncode( because it is much more explicit.

Besides this, it gives a false sense of security, because it only does HTML encoding. Look at this snippet for instance:

function myJavaScriptFunction()
{
    var message = '<%: Person.LastComment %>';
    alert(message);
}

It is not save, because it doesn't use the proper encoding. The following is safe:

function myJavaScriptFunction()
{
    var message = '<%: AntiXss.JavaScriptEncode(Person.LastComment) %>';
    alert(message);
}

Much more explicit.

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