当前位置: 移动技术网 > IT编程>开发语言>.net > 【转载】C#检测客户端输入的内容是否含有危险字符串

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

2019年03月23日  | 移动技术网IT编程  | 我要评论

歌手第二季第九期,烈火如歌2txt下载,鲁敏宇整容前后

用户在客户端提交的内容有时候并不可信,如果客户端提交的内容中含有危险字符串信息,则很有可能造成应用程序安全性问题,如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);
            }
        }

 

备注:原文转载自。

用户在客户端提交的内容有时候并不可信,如果客户端提交的内容中含有危险字符串信息,则很有可能造成应用程序安全性问题,如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);
            }
        }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网