如何编码html,省去了安全的html

时间:2010-09-10 18:25:14

标签: xss html-encode

来自数据库的我的数据可能包含一些html。如果我使用

string dataFromDb = "Some text<br />some more <br><ul><li>item 1</li></ul>";
HttpContext.Current.Server.HtmlEncode(dateFromDb);

然后一切都被编码,我在屏幕上看到了安全的Html。

但是,我希望能够执行上面dataFromDb中提到的安全html。

我想我正在尝试创建白名单以进行检查。

  • 我该怎么做呢?
  • 有没有可以做到这一点的正则表达式?

2 个答案:

答案 0 :(得分:0)

您应该使用Microsoft AntiXSS库。我相信最新版本可用here。具体来说,您需要使用GetSafeHtmlFragment方法。

答案 1 :(得分:0)

结帐this article AntiXSS library也值得一看