相关疑难解决方法(0)

如何使用C#来清理html页面上的输入?

是否有库或可接受的方法来清理html页面的输入?

在这种情况下,我有一个只有姓名,电话号码和电子邮件地址的表单.

代码必须是C#.

例如:

"<script src='bobs.js'>John Doe</script>" 应该成为 "John Doe"

c# html-sanitizing antixsslibrary

36
推荐指数
5
解决办法
3万
查看次数

HTML Agility Pack strip标签不在白名单中

我正在尝试创建一个删除不在白名单中的html标签和属性的函数.我有以下HTML:

<b>first text </b>
<b>second text here
       <a>some text here</a>
 <a>some text here</a>

 </b>
<a>some twxt here</a>
Run Code Online (Sandbox Code Playgroud)

我正在使用HTML敏捷包,到目前为止我的代码是:

static List<string> WhiteNodeList = new List<string> { "b" };
static List<string> WhiteAttrList = new List<string> { };
static HtmlNode htmlNode;
public static void RemoveNotInWhiteList(out string _output, HtmlNode pNode, List<string> pWhiteList, List<string> attrWhiteList)
{

 // remove all attributes not on white list
 foreach (var item in pNode.ChildNodes)
 {
  item.Attributes.Where(u => attrWhiteList.Contains(u.Name) == false).ToList().ForEach(u => RemoveAttribute(u));

 }

 // remove all html and their innerText …
Run Code Online (Sandbox Code Playgroud)

c# tags sanitize html-parsing html-agility-pack

30
推荐指数
2
解决办法
2万
查看次数

如何将HTML保存到数据库并正确检索它

这些天学习安全性:)
我需要允许用户在表单中输入文本并允许他们使用一些HTML标签:粗体,斜体,列表等,并防止他们添加一些危险的JavaScript代码.
所以我使用这个白名单实现来清理HTML.
但我仍然对如何以正确的方式保存和显示它感到困惑.
所以我在这做了什么:
型号:

public class Post
    {
        [AllowHtml]
        public string Data { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost, ActionName("Create")]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Post model)
        {
            // Decode model.Data as it is Encoded after post
            string decodedString = HttpUtility.HtmlDecode(model.Data);
            // Clean HTML
            string sanitizedHtmlText =  HtmlUtility.SanitizeHtml(decodedString);

            string encoded = HttpUtility.HtmlEncode(sanitizedHtmlText);
Run Code Online (Sandbox Code Playgroud)

视图:

@using (Html.BeginForm("Create", "Home", FormMethod.Post)) {    
    @Html.AntiForgeryToken()
    @Html.TextAreaFor(a=>a.Data)
    <input type="submit" value="submit" />
}
Run Code Online (Sandbox Code Playgroud)

所以当我发布表格时,我看到:

<p>Simple <em><strong>whitelist</strong> </em>test:</p>
<ul>
<li>t1</li>
<li>t2</li>
</ul>
<p>Image:</p>
<p>&lt;img src="http://metro-portal.hr/img/repository/2010/06/medium/hijena_shutter.jpg" /&gt;</p>
Run Code Online (Sandbox Code Playgroud)

Becaouse …

xss asp.net-mvc razor asp.net-mvc-4

14
推荐指数
1
解决办法
2万
查看次数