如何使用 AntiXSS 但保持输出可读?
我正在使用 Microsoft 的 AntiXSS 库对网页上显示的内容进行 HtmlEncode。
问题是它编码的内容比我想要的要多。例如,它对冒号“:”进行编码。
有没有办法对 HtmlEncode 进行编码,但不能对某些字符进行编码?
一个例子是,如果我有一个类似“RE:电子邮件主题行”的字符串,是否可以使用 AntiXSS 库对该字符串进行编码,但不会显示为“RE:电子邮件主题行”。我希望显示“:”,但仍想对输出进行编码。
更新: 问题是我使用的是 Razor 并且“@”已经对输出进行了编码。因此,通过使用“@”和 AntiXSS Html.Encode 调用,我进行了双重编码。
I'm using Microsoft's AntiXSS library to HtmlEncode content being displayed on a web page.
The problem is it encodes more than I want it to. For example it encodes the colon ":".
Is there a way to HtmlEncode, but not certain characters?
An example is if I have a string like "RE: email subject line", is it possible to use the AntiXSS library to encode that string but have it not show up as "RE: email subject line". I want the ":" to show up but still want to encode the output.
Update:
The problem was that I was using Razor and the "@" already encodes output. So by using both the "@" and the AntiXSS Html.Encode call I was double encoding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在屏幕上看到编码结果如您的评论所示,那么您正在双重编码。 :是冒号,这将是 AntiXSS 的输出,但之后的内容是对 & 进行编码。到&;
这是做什么的取决于你正在使用什么。如果您使用的是 WebForms,那么默认情况下,一堆控件将对文本和其他属性进行编码,并且停止此操作取决于控件。如果您使用 MVC,则 <%: 在将您传递的内容添加到输出流之前对其执行编码。如果您使用的是 MVC3,您可以替换默认编码器 (AntiXSS beta 4.1,或自己动手(如果您不想使用 beta),或者通过输出 HtmlString。
在 .NET 4.5 AntiXSS 的核心部分将内置并通过配置开关进行配置。
If you are seeing the encoding results on screen as your comments seem to indicate then you are double encoding. : is the colon, and that would be the output of AntiXSS, but something after that is encoding the & to &
What is doing this depends on what you're using. If you're using WebForms then a bunch of the controls will encode Text and other properties by default, and stopping this depends on the control. If you're using MVC the <%: performs encoding of what you're passing before adding it to the output stream. You can either, if you're using MVC3 replace the default encoders (AntiXSS beta 4.1, or roll your own if you don't want to use a beta), or tell MVC you're outputting an already encoding string by outputting an HtmlString.
In .NET 4.5 the core parts of AntiXSS will be built in and configurable via a config switch.