下载地址:
http://www.microsoft.com/en-us/download/details.aspx?id=28589
原来的代码是:
string safeHtml = AntiXss.GetSafeHtmlFragment(html);
Console.WriteLine(safeHtml);
但现在,你会发现会提示:
Microsoft.Security.Application.Antixss已过时,
This class has been deprecated. Please use Microsoft.Security.Application.Encoder instead.
4.0以后的版本,已经将AntiXss中的HtmlSanitizationLibrary独立出来,使用时需要引用HtmlSanitizationLibrary.dll,
using Microsoft.Security.Application;
string safeHtml = Sanitizer.GetSafeHtmlFragment(html);
GetSafeHtml 方法:过滤完整的HTML页面。
GetSafeHtmlFragment 方法:将传入的HTML 视为一个HTML 片段进行过滤。
存在的问题:
AntiXSS会替换除<p><span>等之外的所有HTML标记,无法实现仅过滤script等标记。
如果采用正则表达式来过滤,内容较多时效率较低。黑名单方式的过滤很容易有漏网之鱼,还是采用白名单方式更安全些!
更多XSS方面的内容,可以阅读:
https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29
一篇清理HTML的文章也很值得参考: