转换HTML危险字符的方法

哥斯达黎家 2009-03-04 04:51:04
转换HTML危险字符的代码

比如说我在文本框里有写
<script></script>


我提交以后转换成
<script></script>
也就是HTML格式的。

过滤字符的方法
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxd024 2009-03-04
  • 打赏
  • 举报
回复
帮顶
bj890 2009-03-04
  • 打赏
  • 举报
回复
validateRequest="false"
bolome 2009-03-04
  • 打赏
  • 举报
回复
帮顶,顺便学习.
uddwfn521 2009-03-04
  • 打赏
  • 举报
回复
汗.. 没看清
如果只是 转 > 什么的 这个就行
Server.HtmlEncode()
uddwfn521 2009-03-04
  • 打赏
  • 举报
回复

/// <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;
}
zgke 2009-03-04
  • 打赏
  • 举报
回复
string _Temp = "<script></script> ";

string _OkText =_Temp.Replace("<","<").Replace(">",">");

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧