C# 中的 HtmlEncode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1631819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HtmlEncode in C#
提问by Cornel
Is there any way to html encode some text without using System.Web.HttpUtility.HtmlEncode method? I want to deploy my desktop application using .NET 3.5 Client Profile and the problem is that System.Web.dll it's not part of the .NET 3.5 Client Profile so I have to find an workaround.
有没有办法在不使用 System.Web.HttpUtility.HtmlEncode 方法的情况下对某些文本进行 html 编码?我想使用 .NET 3.5 Client Profile 部署我的桌面应用程序,问题是 System.Web.dll 它不是 .NET 3.5 Client Profile 的一部分,所以我必须找到一个解决方法。
采纳答案by dthrasher
Rick Strahl rolled his own encoding method, due to problems and inconsistencies with .NET's way of encoding things. Check out his post on Html and Uri String Encoding without System.Web.
由于 .NET 编码方式的问题和不一致,Rick Strahl 推出了他自己的编码方法。查看他关于Html 和 Uri String Encoding without System.Web 的帖子。
UPDATE:After checking out the links provided by the other answers, the AntiXSS library provided by Microsoft seems like an ideal solution to this problem. They've made the source of AntiXSS 4.3 available on Codeplex: http://antixss.codeplex.com/
更新:查看其他答案提供的链接后,Microsoft 提供的 AntiXSS 库似乎是此问题的理想解决方案。他们在 Codeplex 上提供了 AntiXSS 4.3 的源代码:http: //antixss.codeplex.com/
The AntiXSS Library includes helpful methods for encoding HTML, URLs, JavaScript, and XML. It's based on a secure whitelist model, so anything not allowed in the specifications is prohibited.
AntiXSS 库包括对 HTML、URL、JavaScript 和 XML 进行编码的有用方法。它基于安全的白名单模型,因此禁止规范中不允许的任何内容。
Note that according to the release notes for 4.3, June 2014, this is the last release that will contain a sanitizer, due to the negative feedback it got from the user community for being overly aggressive. So if it's a sanitizer you want, you should look at AntiSamyor building your own with the HTML agility pack.
请注意,根据 2014 年 6 月 4.3 的发行说明,这是最后一个包含消毒剂的版本,因为它从用户社区那里得到了过于激进的负面反馈。因此,如果它是您想要的消毒剂,您应该查看AntiSamy或使用HTML 敏捷包构建您自己的消毒剂。
回答by blowdart
回答by Joel Fillmore
I'm a fan of the AntiXSS library as well, but its worth mentioning that .net v4 includes a new utility class for encoding in System.dll. So if you have the option of moving to .net v4, you can use the client profile.
我也是 AntiXSS 库的粉丝,但值得一提的是,.net v4 包含一个新的实用程序类,用于在 System.dll 中进行编码。因此,如果您可以选择迁移到 .net v4,则可以使用客户端配置文件。