62,267
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 过滤危险脚本
/// </summary>
/// <param name="htmlCode"></param>
/// <returns></returns>
public string wipeScript(string htmlCode)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\s]", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\s]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\s]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\s]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\s]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
htmlCode = regex1.Replace(htmlCode, ""); //过滤<script></script>标记
htmlCode = regex2.Replace(htmlCode, ""); //过滤href=javascript: (<a>) 属性
htmlCode = regex3.Replace(htmlCode, " _disibledevent="); //过滤其它控件的on...事件
htmlCode = regex4.Replace(htmlCode, ""); //过滤iframe
htmlCode = regex5.Replace(htmlCode, ""); //过滤frameset
return htmlCode;
}