下載地址:
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的文章也很值得參考: