.NET过滤乱码和危险字符

yzone 2009-02-23 04:22:31
急~!!!
...全文
486 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
oumeiren 2009-02-23
  • 打赏
  • 举报
回复
我这里有个方法,基本上都能过滤掉

public static string DelHtmlCode(string html)
{
html = GetString2(html); //先过滤脏话
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", 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);
//System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

//System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

html = regex1.Replace(html, ""); //过滤<script></script>标记
html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
html = regex4.Replace(html, ""); //过滤iframe
html = regex5.Replace(html, ""); //过滤frameset
//html = regex6.Replace(html, ""); //过滤frameset
//html = regex7.Replace(html, ""); //过滤frameset
//html = regex8.Replace(html, ""); //过滤frameset
//html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");

//给图片添加上控制尺寸的方法
string strReg = @"\<img[^\>]+\>";
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(html, strReg, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
for (int i = 0; i < matches.Count; i++)
{
string a = matches[i].ToString(); //原来的图片
if (a.IndexOf("/>") < 0)
{
a = a.Replace(">", "/>");
}
string aa = a.Substring(0, a.Length - 2) + " onload='javascript:DrawImage(this);' >";
html = html.Replace(a, aa);
}

return html;
}
wangxiaofeiwuqiao 2009-02-23
  • 打赏
  • 举报
回复
//查找
protected void Button1_Click(object sender, EventArgs e)
{
string str = TextBox1.Text.Trim();
if (ChkBadWord(str))
{
Response.Write("<script>alert('存在非法字符')</script>");
}
else
{
Response.Write("<script>alert('不存在非法字符')</script>");
}
}


//过滤字符方法
public bool ChkBadWord(string badword)
{
string[] bw = strbadword();
bool isok = false;
foreach (string str in bw)
{
if (!(badword.IndexOf(str) > -1))
{
isok = true;
return isok;
}

}
return isok;
}

//非法字符系列
private string[] strbadword()
{
string[] bad = new string[11];
bad[0] = "'";
bad[1] = "\"";
bad[2] = ";";
bad[3] = "--";
bad[4] = ",";
bad[5] = "!";
bad[6] = "~";
bad[7] = "@";
bad[8] = "#";
bad[9] = "$";
bad[10] = "%";

return bad;
}
hjw01592 2009-02-23
  • 打赏
  • 举报
回复
/// <summary>
/// 过滤文本中的非法字符串
/// </summary>
/// <param name="str">要输入的文本</param>
/// <returns></returns>
public static string HtmlEncode(string str)
{

str = str.Replace("&", "&");
str = str.Replace("<", "<");
str = str.Replace(">", ">");
str = str.Replace("'", "''");
str = str.Replace("*", "");
str = str.Replace("\n", "<br/>");
str = str.Replace("\r\n", "<br/>");
str = str.Replace("select", "");
str = str.Replace("insert", "");
str = str.Replace("update", "");
str = str.Replace("delete", "");
str = str.Replace("create", "");
str = str.Replace("drop", "");
str = str.Replace("delcare", "");
if (str.Trim().ToString() == "") { str = "无"; }
return str.Trim();
}
xutao888 2009-02-23
  • 打赏
  • 举报
回复
用参数方式调用SQL
yzone 2009-02-23
  • 打赏
  • 举报
回复
我想在输入的一个文本框过后,点提交按纽进行验证,在输入的框中要过滤掉HTML标签和乱码以及危险字符等东西!
wuyq11 2009-02-23
  • 打赏
  • 举报
回复
http://www.cnblogs.com/wangying110166/archive/2007/05/28/762764.aspx
http://www.cnblogs.com/coolylh/archive/2006/03/21/355041.html
sykey 2009-02-23
  • 打赏
  • 举报
回复
string charstr = "',;,*,%,and,exec,insert,select,count,chr,mid,master,truncate,char,declare";
string[] strarr = charstr.Split(',');
foreach (string i in strarr)
{
if (str.IndexOf(i) > -1)
{
return false;
}
}
fxcjy 2009-02-23
  • 打赏
  • 举报
回复
楼主真懒,什么也没说
BossFriday 2009-02-23
  • 打赏
  • 举报
回复
过滤什么的危险字符sql还是UI里js的或者其他.
E桶金行业搜索引擎系统(包含多线程客户端蜘蛛系统) V1.6 版发布! E桶金行业搜索引擎 特别适用于超大、中型信息门户、行业门户、电子商务等网站使用。 它是一款真正意义上的搜索引擎:具有自己的蜘蛛、分词、索引、搜索等全套功能。 而且所有功能全部基于web页面完成,您只需要购买一款支持asp.net的虚拟主机就能完成本程序的部署,网站轻松上线。 本程序具有毫秒级高速搜索, 搜索结果相关度排序 。多关键词搜索、超长词条搜索、关键词高亮显示。 本程序包中的Spider.rar文件是客户端蜘蛛插件,用于多线程快速索引网站,并抓取快照。 本程序包中的MsSql.rar文件为MsSql数据库版本附加文件包,如果您想使用sql版本,参照mssql增值包中的说明。 注意:本程序的商业授权分两种版本:Access版本和MsSql版本,如果您的授权是Access版本,那么升级为MsSql版本后,将变为MsSql免费版本,如果想升级为MsSql商业版本,请联系E桶金客服。 2011.06.01升级(1.6版本)的主要内容如下: 1.提供两种蜘蛛客户端索引和网站索引进行合并的策略。(重要) 2.优化编码识别规则,大幅件减少搜索日志乱码现象。(重要) 3.后台可批量删除某一域名下的所有搜引文件的功能!(重要) 4.可过滤某一ip的搜索记录,避免数据库迅速膨胀。(重要) 5.将广告内容的字数限制由100个字符增长到128个字符。 6.升级优化客户端蜘蛛的部分功能:修正入口地址设置中屏蔽的url关键词设置后无效的问题;修正定时更新,间隔更新设置后,按钮无法提交的问题;蜘蛛程序每执行一阶段采集任务后,自动释放cpu和内存,避免蜘蛛一直执行会挂死。   该程序为ASP.NET2.0版本程序,只要空间支持asp.net2.0即可。 需要确保iis的默认文档中包含default.aspx

62,046

社区成员

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

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

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

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