使用 javascript 删除 C# 字符串以及客户端中的 html 标签或脚本标签

发布于 2024-10-14 19:13:17 字数 684 浏览 12 评论 0原文

我需要进行用户输入验证,并且希望在客户端和服务器端都对其进行验证。 我有一个文本框,用户可以在产品上写下他的评论,现在我想做的是验证他的评论是否没有任何像 html 或 javascript 那样的注入。所以我想做的是,在用户点击提交后

1.) 客户端:如果用户输入这种字符串,我将如何执行验证

<a href="">abcd</a> // I will accept only abcd and remove the anchor tag but the abcd should appear as a link
<script type="text/javascript">alert(123);</script> // I will accept only alert(123);as the valid string
<b>abcd</b> // I will display abcd but it must appear bold

2.) 服务器端:与客户端的情况相同,我将删除注入脚本的标签和html标签。 我正在使用 sharepoint 2007,我不确定是否有内置函数可以在 sharepoint api 或 c# 中进行这种验证以进行服务器端验证。

注意:我不想为此软件或任何第三方软件使用 RegEx。我知道这里有很多专家可以帮助我解决这个问题。太感谢了!

I need to do a user input validation, and I want it validated both in the client side and in the server side.
I have ang textbox that the user can write his comment on the product, now what I wanted to do is to validate if his comment doesn't have any injections like html or javascripts. So what I wanted to do, after the user clicks on submit

1.) Client Side: How will I execute a validation like if the user inputs this kinds of string

<a href="">abcd</a> // I will accept only abcd and remove the anchor tag but the abcd should appear as a link
<script type="text/javascript">alert(123);</script> // I will accept only alert(123);as the valid string
<b>abcd</b> // I will display abcd but it must appear bold

2.) Server side: Same situation with the client side, I will remove the tags of the injected script and html tags.
I am using sharepoint 2007, I'm not sure if there is a built-in function to do this kind of validation in sharepoint api or c# for the server side validation.

Note: I don't want to use RegEx for this or any third party software. I know many experts here can help me with this. Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

带刺的爱情 2024-10-21 19:13:17

虽然 RegEx 可能是您最好的选择,但您可以使用它并根据您的喜好进行修改:

public static string StripHtml(this string source)
{
    string[] removeElements = new string[] { "a", "script" };
    string _newString = source;
    foreach (string removeElement in removeElements)
    {
        while (_newString.ToLower().Contains("<" + removeElement.ToLower()))
        {
            _newString = _newString.Substring(0, _newString.ToLower().IndexOf("<" + removeElement.ToLower())) + _newString.Substring(_newString.ToLower().IndexOf("</" + removeElement.ToLower() + ">") + removeElement.Length + 3);
        }
    }
    return _newString;
}

您将使用 string clean = txtInput.Text.StripHtml();

While RegEx is probably your best bet, you can use this and modify to your liking:

public static string StripHtml(this string source)
{
    string[] removeElements = new string[] { "a", "script" };
    string _newString = source;
    foreach (string removeElement in removeElements)
    {
        while (_newString.ToLower().Contains("<" + removeElement.ToLower()))
        {
            _newString = _newString.Substring(0, _newString.ToLower().IndexOf("<" + removeElement.ToLower())) + _newString.Substring(_newString.ToLower().IndexOf("</" + removeElement.ToLower() + ">") + removeElement.Length + 3);
        }
    }
    return _newString;
}

You'll use string clean = txtInput.Text.StripHtml();

So要识趣 2024-10-21 19:13:17

我不确定是否要为此创建验证。但您可以使用此函数以编程方式删除标签。

使用此函数从用户输入的文本框值中删除 Html 标记

public static string StripHtml(string html, bool allowHarmlessTags)
{
   if (html == null || html == string.Empty)
     return string.Empty; 

   if (allowHarmlessTags)
     return System.Text.RegularExpressions.Regex.Replace(html, "", string.Empty); 

   return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
}

I am not sure about creating an validation for this. But you can programtically remove the tags using this function.

Use this function to remove the Html tage from the textbox value that user has input

public static string StripHtml(string html, bool allowHarmlessTags)
{
   if (html == null || html == string.Empty)
     return string.Empty; 

   if (allowHarmlessTags)
     return System.Text.RegularExpressions.Regex.Replace(html, "", string.Empty); 

   return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
}
梦与时光遇 2024-10-21 19:13:17

如果您想防止 JavaScript 注入攻击,只需对用户输入进行编码 Server.HtmlEncode(message)
但如果您需要清理一些标签,那么 Omar Al Zabir 写了一篇好文章 Convert HTML to XHTML and Clean不必要的标签和属性

// Encode the string input
StringBuilder sb = new StringBuilder(
                        HttpUtility.HtmlEncode(htmlInputTxt.Text));

// Selectively allow <b> and <i>
sb.Replace("<b>", "<b>");
sb.Replace("</b>", "");
sb.Replace("<i>", "<i>");
sb.Replace("</i>", "");
Response.Write(sb.ToString());

我还想建议您检查AntiSamy.NET项目,但我自己没有尝试过。

If you want prevent javascript injection attacks just encode user input Server.HtmlEncode(message).
But if you need to clean some tags then Omar Al Zabir wrote good article Convert HTML to XHTML and Clean Unnecessary Tags and Attributes

// Encode the string input
StringBuilder sb = new StringBuilder(
                        HttpUtility.HtmlEncode(htmlInputTxt.Text));

// Selectively allow <b> and <i>
sb.Replace("<b>", "<b>");
sb.Replace("</b>", "");
sb.Replace("<i>", "<i>");
sb.Replace("</i>", "");
Response.Write(sb.ToString());

I also would like to recomand you check AntiSamy.NET project but I didn't try it by myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文