ASP.NET 4 中是否可以默认打开 HTML 编码?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,幸运的是。
如果您这样配置,您的开发人员将养成使用
<%= ... %>
而不是编码的习惯。如果他们随后从事不同的(正常)项目,他们最终会忘记对输出进行编码。
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)
就我个人而言,我不喜欢这个新的
<%:
语法。我宁愿使用<%= HttpUtility.HtmlEncode(
因为它更加明确。除此之外,它还给人一种错误的安全感,因为它只进行 HTML 编码。例如看这个代码片段:
它不会保存,因为它没有使用正确的编码 以下是安全的:
更明确。
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:
It is not save, because it doesn't use the proper encoding. The following is safe:
Much more explicit.