最近有涉及到要防止用户在网页文本框中输入具有风险的敏感字符所以特地编写了一套针对用户输入的字符进行安全过滤的一个方法,在后台接收到用户输入的字符后调用执行该方法即可完成过滤操作,主要使用正则来匹配并替换掉敏感字符!

    /// <summary>
/// obj向string转换,替换具有风险的敏感字符并去除多余的空格;
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string RequestFilter(this object o)
{
if (o == DBNull.Value || o == null)
{
return "";
}
else
{
string str = o.ToString().Trim();
//删除脚本
str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
str = Regex.Replace(str, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<!--.*", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&#(\d+);", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase); //删除与数据库相关的词
str = Regex.Replace(str, "select", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "insert", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "count''", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "asc", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "mid", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "char", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "exec master", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net localgroup administrators", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "and", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net user", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "or", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "delete", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "drop", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "script", "", RegexOptions.IgnoreCase); str = str.Replace("<", "");
str = str.Replace(">", "");
str = str.Replace("*", "");
str = str.Replace("--", "");
str = str.Replace("?", "");
str = str.Replace(",", "");
str = str.Replace("/", "");
str = str.Replace(";", "");
str = str.Replace("*/", "");
str = str.Replace("\r\n", "");
return str;
}
}

C#-防止用户输入具有风险的敏感字符的更多相关文章

  1. C 统计用户输入的总行数和字符长度

    C 统计用户输入的总行数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; printf ...

  2. strtr对用户输入的敏感词汇进行过滤

    /** * 过滤用户输入的基本数据,防止script攻击 * * @access public * @return string */ function compile_str($str) { $ar ...

  3. [ PyQt入门教程 ] PyQt5基本控件使用:消息弹出、用户输入、文件对话框

    本文主要介绍PyQt界面实现中常用的消息弹出对话框.提供用户输入的输入框.打开文件获取文件/目录路径的文件对话框.学习这三种控件前,先想一下它们使用的主要场景: 1.消息弹出对话框.程序遇到问题需要退 ...

  4. 防御XSS攻击-encode用户输入内容的重要性

    一.开场先科普下XSS 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶 ...

  5. python学习笔记(基础二:注释、用户输入、格式化输出)

    注释 单行:# 多行:上下各用3个连续单引号或双引号 3个引号除了多行注释,还可以打印多行 举例: msg = ''' name = "Alex Li" name2 = name ...

  6. 第4章 Java接收用户输入

    第4章 Java接收用户输入 1.输入 使用Scanner工具类可以换取用户输入的数据Scanner类位于java.util包中,使用时需要导入此包使用步骤: 1.导入java.util.Scanne ...

  7. Python学习【第四篇】用户输入及判断

    用户输入: 例1.写一个用户输入密码的小程序,流程如下: 1.用户输入自己的用户名 2.打印"Hello+用户名" #!/usr/bin/env python #name = ra ...

  8. 使用scanner工具类来获取用户输入的信息

    使用scanner工具类来获取用户输入的成绩信息. 步骤:1.导入java.util.Scanner包 2.创建Scanner对象 3.接受并保存用户输入的值 例子:通过用户输入来获取学生成绩 pac ...

  9. alertDialog创建登陆界面,判断用户输入

    alertDialog创建登陆界面,需要获取用户输入的用户名和密码,获取控件对象的时候不能像主布局文件那样获得, 需要在onClickListener中获取,代码如下: public boolean ...

随机推荐

  1. hdu2830 可交换行的最大子矩阵

    题意:       求最大子矩阵,但是相邻的列之间可以相互交换... 思路:       回想下固定的情况,记得那种情况是开俩个数组 L[i] ,R[i],记录小于等于i的最左边和最右边在哪个位置,对 ...

  2. Known Notation 39届亚洲赛牡丹江站K题

    题意:       题意,哎!说道题意就蛋疼啊,比赛的时候就愣是把这个题目读成数字可以随意组合,比如123 可以拆成1 23 ,12 3 ,1 2 3,结果显然,水题当神题,各种想不出来,然后就显然的 ...

  3. 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第n次落地时,共经过多少米?第n次反弹多高?(n<=10)

    单纯考逻辑 题目: 一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下,求它在第n次落地时,共经过多少米?第n次反弹多高?(n<=10) 输入描述: 一行,一个整数n (1< ...

  4. Method Overlonding

    The method overloading is using one single method name with different parameters to created differen ...

  5. Java 中 RMI 的使用

    RMI 介绍 RMI (Remote Method Invocation) 模型是一种分布式对象应用,使用 RMI 技术可以使一个 JVM 中的对象,调用另一个 JVM 中的对象方法并获取调用结果.这 ...

  6. 使用 cmake 来搭建跨平台的应用程序框架:C语言版本

    目录 一.前言 二.示例代码说明 1. 功能描述 2. 文件结构 3. cmake 构建步骤 4. Utils 目录说明 5. Application 目录说明 三.Linux 系统下操作步骤 1. ...

  7. c++debug&注意事项 自用 持续更新

    cin后回车程序直接退出: 加system("pause");在return 0;前面 C++ 控制cout输出的小数位数 C++中的cout.setf().cout.precis ...

  8. SparkSQL电商用户画像(五)之用户画像开发(客户基本属性表)

    7.电商用户画像开发 7.1用户画像--数据开发的步骤 u 数据开发前置依赖 -需求确定 pv uv topn -建模确定表结构 create table t1(pv int,uv int,topn ...

  9. js EventSource 长链接

    有这么一个场景:服务端处理数据,响应比较慢,为了不让用户体会到网页没有反应,服务端需要把处理的每一步操作返回给前端,前端实时进行打印. 1.ajax 轮询 <script> setInte ...

  10. 一文搞懂:java中的VO、PO、BO、DAO、POJO

    针对java工程里的各种带O的对象,进行分析,了解各自的作用. PO:persistent object,持久对象.与数据库里表字段一一对应.PO是一些属性,以及set和get方法组成.一般情况下,一 ...