欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【转载】C#检测客户端输入的内容是否含有危险字符串

程序员文章站 2024-01-11 18:39:04
用户在客户端提交的内容有时候并不可信,如果客户端提交的内容中含有危险字符串信息,则很有可能造成应用程序安全性问题,如SQL注入风险等。因此在接收客户端提交过来的数据后,我们首先需要判断数据中是否含有危险字符信息,如果有则可以提前处理掉,如去除一些SQL注入攻击的关键字等。 校验的方法封装如下: 备注 ......

用户在客户端提交的内容有时候并不可信,如果客户端提交的内容中含有危险字符串信息,则很有可能造成应用程序安全性问题,如sql注入风险等。因此在接收客户端提交过来的数据后,我们首先需要判断数据中是否含有危险字符信息,如果有则可以提前处理掉,如去除一些sql注入攻击的关键字等。

校验的方法封装如下:

 /// <summary>
        /// 检测客户输入的字符串是否有效,并将原始字符串修改为有效字符串或空字符串。
        /// 当检测到客户的输入中有攻击性危险字符串,则返回false,有效返回true。
        /// </summary>
        /// <param name="input">要检测的字符串</param>
        public static bool isvalidinput(ref string input)
        {
            try
            {
                if (isnullorempty(input))
                {
                    //如果是空值,则跳出
                    return true;
                }
                else
                {
                    //替换单引号
                    input = input.replace("'", "''").trim();
                    //检测攻击性危险字符串
                    string teststring = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
                    string[] testarray = teststring.split('|');
                    foreach (string teststr in testarray)
                    {
                        if (input.tolower().indexof(teststr) != -1)
                        {
                            //检测到攻击字符串,清空传入的值
                            input = "";
                            return false;
                        }
                    }
                    //未检测到攻击字符串
                    return true;
                }
            }
            catch (exception ex)
            {
                throw new exception(ex.message);
            }
        }

 

备注:原文转载自c#检测客户端输入的内容是否含有危险字符串_it技术小趣屋

用户在客户端提交的内容有时候并不可信,如果客户端提交的内容中含有危险字符串信息,则很有可能造成应用程序安全性问题,如sql注入风险等。因此在接收客户端提交过来的数据后,我们首先需要判断数据中是否含有危险字符信息,如果有则可以提前处理掉,如去除一些sql注入攻击的关键字等。

校验的方法封装如下:

  /// <summary>
        /// 检测客户输入的字符串是否有效,并将原始字符串修改为有效字符串或空字符串。
        /// 当检测到客户的输入中有攻击性危险字符串,则返回false,有效返回true。
        /// </summary>
        /// <param name="input">要检测的字符串</param>
        public static bool isvalidinput(ref string input)
        {
            try
            {
                if (isnullorempty(input))
                {
                    //如果是空值,则跳出
                    return true;
                }
                else
                {
                    //替换单引号
                    input = input.replace("'", "''").trim();

                    //检测攻击性危险字符串
                    string teststring = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
                    string[] testarray = teststring.split('|');
                    foreach (string teststr in testarray)
                    {
                        if (input.tolower().indexof(teststr) != -1)
                        {
                            //检测到攻击字符串,清空传入的值
                            input = "";
                            return false;
                        }
                    }

                    //未检测到攻击字符串
                    return true;
                }
            }
            catch (exception ex)
            {
                throw new exception(ex.message);
            }
        }