C# 方法剥离不在安全列表中的HTML标记

C# 方法剥离不在安全列表中的HTML标记,c#,asp.net,C#,Asp.net,是否有一种方法可以去除所有不在安全标签列表中的HTML标签?如果没有,正则表达式是什么?实现它的方法是什么 我正在寻找类似PHP函数的东西 使用XML解析器: 使用XML解析器: 您可以使用MS AntiXSS库来清理潜在的可执行HTML。请看这里: 您可以使用MS AntiXSS库来清理潜在的可执行HTML。请看这里: NullUserException的答案是完美的,我做了一个小小的扩展方法来实现它,如果其他人需要,我会在这里发布 using System; using System.Co

是否有一种方法可以去除所有不在安全标签列表中的HTML标签?如果没有,正则表达式是什么?实现它的方法是什么

我正在寻找类似PHP函数的东西

使用XML解析器:

使用XML解析器:

您可以使用MS AntiXSS库来清理潜在的可执行HTML。请看这里:


您可以使用MS AntiXSS库来清理潜在的可执行HTML。请看这里:


NullUserException的答案是完美的,我做了一个小小的扩展方法来实现它,如果其他人需要,我会在这里发布

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace Extenders
{
    public static class StringExtender
    {
        internal static void ParseHtmlDocument(XmlDocument doc, XmlNode root, string[] allowedTags, string[] allowedAttributes, string[] allowedStyleKeys)
        {
            XmlNodeList nodes;

            if (root == null) root = doc.ChildNodes[0];
            nodes = root.ChildNodes;

            foreach (XmlNode node in nodes)
            {
                if (!(allowedTags.Any(x => x.ToLower() == node.Name.ToLower())))
                {
                    var safeNode = doc.CreateTextNode(node.InnerText);
                    root.ReplaceChild(safeNode, node);
                }
                else
                {
                    if (node.Attributes != null)
                    {
                        var attrList = node.Attributes.OfType<XmlAttribute>().ToList();
                        foreach (XmlAttribute attr in attrList)
                        {
                            if (!(allowedAttributes.Any(x => x.ToLower() == attr.Name)))
                            {
                                node.Attributes.Remove(attr);
                            }
                            // TODO: if style is allowed, check the allowed keys: values
                        }
                    }
                }

                if (node.ChildNodes.Count > 0)
                    ParseHtmlDocument(doc, node, allowedTags, allowedAttributes, allowedStyleKeys);
            }
        }

        public static string ParseSafeHtml(this string input, string[] allowedTags, string[] allowedAttributes, string[] allowedStyleKeys)
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("<span>" + input + "</span>");

            ParseHtmlDocument(xmlDoc, null, allowedTags, allowedAttributes, allowedStyleKeys);

            string result;

            using (var sw = new StringWriter())
            {
                using (var xw = new XmlTextWriter(sw))
                    xmlDoc.WriteTo(xw);

                result = sw.ToString();
            }

            return result.Substring(6, result.Length - 7);
        }
    }
}
使用:

var x = "<b>allowed</b><b class='text'>allowed attr</b><b id='5'>not allowed attr</b><i>not all<b>o</b>wed tag</i>".ParseSafeHtml((new string[] { "b", "#text" }), (new string[] { "class" }), (new string[] { }));
哪些产出:

<b>allowed</b><b class='text'>allowed attr</b><b>not allowed attr</b>not allowed tag

如果元素不被允许,它将获取innerText并拉出标记,删除所有内部标记。

NullUserException答案很完美,我做了一个小小的扩展方法来实现它,如果其他人需要,我将在这里发布

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace Extenders
{
    public static class StringExtender
    {
        internal static void ParseHtmlDocument(XmlDocument doc, XmlNode root, string[] allowedTags, string[] allowedAttributes, string[] allowedStyleKeys)
        {
            XmlNodeList nodes;

            if (root == null) root = doc.ChildNodes[0];
            nodes = root.ChildNodes;

            foreach (XmlNode node in nodes)
            {
                if (!(allowedTags.Any(x => x.ToLower() == node.Name.ToLower())))
                {
                    var safeNode = doc.CreateTextNode(node.InnerText);
                    root.ReplaceChild(safeNode, node);
                }
                else
                {
                    if (node.Attributes != null)
                    {
                        var attrList = node.Attributes.OfType<XmlAttribute>().ToList();
                        foreach (XmlAttribute attr in attrList)
                        {
                            if (!(allowedAttributes.Any(x => x.ToLower() == attr.Name)))
                            {
                                node.Attributes.Remove(attr);
                            }
                            // TODO: if style is allowed, check the allowed keys: values
                        }
                    }
                }

                if (node.ChildNodes.Count > 0)
                    ParseHtmlDocument(doc, node, allowedTags, allowedAttributes, allowedStyleKeys);
            }
        }

        public static string ParseSafeHtml(this string input, string[] allowedTags, string[] allowedAttributes, string[] allowedStyleKeys)
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("<span>" + input + "</span>");

            ParseHtmlDocument(xmlDoc, null, allowedTags, allowedAttributes, allowedStyleKeys);

            string result;

            using (var sw = new StringWriter())
            {
                using (var xw = new XmlTextWriter(sw))
                    xmlDoc.WriteTo(xw);

                result = sw.ToString();
            }

            return result.Substring(6, result.Length - 7);
        }
    }
}
使用:

var x = "<b>allowed</b><b class='text'>allowed attr</b><b id='5'>not allowed attr</b><i>not all<b>o</b>wed tag</i>".ParseSafeHtml((new string[] { "b", "#text" }), (new string[] { "class" }), (new string[] { }));
哪些产出:

<b>allowed</b><b class='text'>allowed attr</b><b>not allowed attr</b>not allowed tag

如果不允许使用该元素,它将获取innerText并拉出标记,删除所有内部标记。

我喜欢这篇文章。我从不厌倦它。太好了,这正是我所需要的。我喜欢那个帖子。我从未厌倦过。太好了,这正是我所需要的。这个问题到底是什么?这怎么可能和我的问题有关呢?你能解释一下吗?这个问题有可能重复?这怎么可能和我的问题有关呢?你能解释一下吗?+1!但有一个问题..为什么不删除/检查或忽略?+1!但有一个问题..为什么忽略或不删除/检查?