下面是两个过滤的方法

/// <summary>
/// 此处过滤危险HTML方法
/// </summary>
/// <param name="html">html</param>
/// <returns></returns>
private string FilterHTML(string html)
{
if (html == null)
return ""; //过滤 script
Regex regex_script1 = new Regex("(<script[//s//S]*?///script//s*>)", RegexOptions.IgnoreCase);
Regex regex_script2 = new Regex("(<(script[//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_script1.Replace(html, "");
html = regex_script1.Replace(html, ""); //过滤 <iframe> 标签
Regex regex_iframe1 = new Regex("(<iframe [//s//S]+<iframe//s*>)", RegexOptions.IgnoreCase);
Regex regex_iframe2 = new Regex("(<(iframe [//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_iframe1.Replace(html, "");
html = regex_iframe2.Replace(html, ""); //过滤 <frameset> 标签
Regex regex_frameset1 = new Regex("(<frameset [//s//S]+<frameset //s*>)", RegexOptions.IgnoreCase);
Regex regex_frameset2 = new Regex("(<(frameset [//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_frameset1.Replace(html, "");
html = regex_frameset2.Replace(html, ""); //过滤 <frame> 标签
Regex regex_frame1 = new Regex("(<frame[//s//S]+<frame //s*>)", RegexOptions.IgnoreCase);
Regex regex_frame2 = new Regex("(<(frame[//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_frame1.Replace(html, "");
html = regex_frame2.Replace(html, ""); //过滤 <form> 标签
Regex regex_form1 = new Regex("(<(form [//s//S]*?)>)", RegexOptions.IgnoreCase);
Regex regex_form2 = new Regex("(<(/form[//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_form1.Replace(html, "");
html = regex_form2.Replace(html, ""); //过滤 on: 的事件
//过滤on 带单引号的 过滤on 带双引号的 过滤on 不带有引号的
string regOn = @"<[//s//S]+ (on)[a-zA-Z]{4,20} *= *[//S ]{3,}>";
string regOn2 = @"((on)[a-zA-Z]{4,20} *= *'[^']{3,}')|((on)[a-zA-Z]{4,20} *= */""[^/""]{3,}/"")|((on)[a-zA-Z]{4,20} *= *[^>/ ]{3,})";
html = GetReplace(html, regOn, regOn2, ""); //过滤 javascript: 的事件
regOn = @"<[//s//S]+ (href|src|background|url|dynsrc|expression|codebase) *= *[ /""/']? *(javascript:)[//S]{1,}>";
regOn2 = @"(' *(javascript|vbscript):([//S^'])*')|(/"" *(javascript|vbscript):[//S^/""]*/"")|([^=]*(javascript|vbscript):[^/> ]*)";
html = GetReplace(html, regOn, regOn2, ""); return html;
} /// <summary>
/// 正则双重过滤
/// </summary>
/// <param name="content"></param>
/// <param name="splitKey1"></param>
/// <param name="splitKey2"></param>
/// <param name="newChars"></param>
/// <returns></returns>
private string GetReplace(string content, string splitKey1, string splitKey2, string newChars)
{
//splitKey1 第一个正则式匹配 //splitKey2 匹配结果中再次匹配进行替换 if (splitKey1 != null && splitKey1 != "" && splitKey2 != null && splitKey2 != "")
{
Regex rg = new Regex(splitKey1);
System.Text.RegularExpressions.MatchCollection mc = rg.Matches(content); foreach (System.Text.RegularExpressions.Match mc1 in mc)
{
string oldChar = mc1.ToString();
string newChar = new Regex(splitKey2, RegexOptions.IgnoreCase).Replace(oldChar, newChars);
content = content.Replace(oldChar, newChar);
}
return content;
}
else
{
if (splitKey2 != null && splitKey2 != "")
{
Regex rg = new Regex(splitKey2, RegexOptions.IgnoreCase);
return rg.Replace(content, newChars);
}
}
return content;
}

使用的时候

this.content.InnerHtml = FilterHTML(studentQuestionInfo.Description);

C# 正则表达式过滤危险HTML的更多相关文章

  1. PHP过滤指定字符串,过滤危险字符

    安全过滤函数,用于过滤危险字符 function safe_replace($string) {  $string = str_replace(' ','',$string);  $string = ...

  2. 【转载】C#防SQL注入过滤危险字符信息

    不过是java开发还是C#开发或者PHP的开发中,都需要关注SQL注入攻击的安全性问题,为了保证客户端提交过来的数据不会产生SQL注入的风险,我们需要对接收的数据进行危险字符过滤来防范SQL注入攻击的 ...

  3. Java正则表达式过滤出字母、数字和中文

    原文:http://blog.csdn.net/k21325/article/details/54090066 1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式 [^(A ...

  4. java 使用正则表达式过滤HTML中标签

    /** * 去掉文本中的html标签 * * @param inputString * @return */ public static String html2Text(String inputSt ...

  5. java正则表达式过滤html标签

    import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <p> * Title: HTML相关的正则表达 ...

  6. asp.net正则表达式过滤标签和数据提取

    无论什么语言,正则表达式的处理方法都是非常灵活.高效的,尤其是对某些字符串的抓取.过滤方面,更显其优势. 正则表达式的写法通常比较简单,几行短代码便能轻松完成看似很复杂的事情,更值得称赞的是,它的执行 ...

  7. 正则表达式过滤联系方式,微信手机号QQ等

    有些输入不允许用户输入联系方式.可以使用以下正则表达式来判断是否输入敏感信息 var reg = new RegExp("(微信|QQ|qq|weixin|1[0-9]{10}|[a-zA- ...

  8. MySQL手工注入进阶篇——突破过滤危险字符问题

    当我们在进行手工注入时,有时候会发现咱们构造的危险字符被过滤了,接下来,我就教大家如何解决这个问题.下面是我的实战过程.这里使用的是墨者学院的在线靶场.咱们直接开始. 第一步,判断注入点. 通过测试发 ...

  9. MYSQL使用正则表达式过滤数据

    一.正则与LIKE的区别 Mysql的正则表达式仅仅使SQL语言的一个子集,可以匹配基本的字符.字符串.例如:select * from wp_posts where post_name REGEXP ...

随机推荐

  1. Windows server 2008 SSD性能测试

    过渡到windows 7.windows8是趋势,老迈的windows xp .windows server 2003已经快到淘汰的阶段,安装了windows server 2008 R2 ,测试了下 ...

  2. [leetcode]Next Permutation @ Python

    原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...

  3. [leetcode]Subsets II @ Python

    原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...

  4. [PowerShell Utils] Automatically Change DNS and then Join Domain

    I would like to start a series of blog posts sharing PowerShell scripts to speed up our solution ope ...

  5. Edit Distance leetcode java

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

  6. 转:UFLDL_Tutorial 笔记(deep learning绝佳的入门资料 )

    http://blog.csdn.net/dinosoft/article/details/50103503 推荐一个deep learning绝佳的入门资料 * UFLDL(Unsupervised ...

  7. Python机器学习——线性模型

    http://www.dataguru.cn/portal.php?mod=view&aid=3514 摘要 : 最近断断续续地在接触一些python的东西.按照我的习惯,首先从应用层面搞起, ...

  8. 向windows添加环境变量

    以NASM为例,软件安装完毕后,启动Windows操作系统的命令窗口,在安装目录(比如C:\Program Files\NASM)下运行nasm是ok的,但是在其他任意目录下运行nasm就会报错. 这 ...

  9. sql server2008R2 无法连接到WMI提供程序。你没有权限或者该服务器无法访问

    在自己的Win8.1的系统在安装了Vs2013和Sqlserver2008R2 今天在打开ssms的时候发现连接不上数据库,且出现了以下问题 然后打开Sqlserver配置管理器准备看看sqlserv ...

  10. Hibernate中得fetch

    fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例:fetch = "select"是 ...