ASP.NET 检测并替换用户输入的恶意字符

时间:2015/12/3 17:00:00来源:互联网 作者:flyso 点击: 901 次

/// <summary>
        /// 该方法检测并替换用户输入的恶意字符
        /// </summary>
        /// <param name="text">用户输入的文字 </param>        
        /// <returns>返回验证后的文字 </returns>
        public static string InputText(string text)
        {
            text = text.ToLower().Trim();
            if (string.IsNullOrEmpty(text))
            {
                return string.Empty;
            }
            else
            {
                text = Regex.Replace(text, ".", "。");
                //text = Regex.Replace(text, "=", "");
                //text = Regex.Replace(text, "%", "");
                text = Regex.Replace(text, "'", "’");
                //text = Regex.Replace(text, "select", "");
                //text = Regex.Replace(text, "insert", "");
                //text = Regex.Replace(text, "delete", "");
                //text = Regex.Replace(text, "or", "");              
                //text = Regex.Replace(text, "--", "");
                //text = Regex.Replace(text, "and", "");
                //text = Regex.Replace(text, "where", "");
                //text = Regex.Replace(text, "update", "");              
                //text = Regex.Replace(text, "master", "");
                //text = Regex.Replace(text, "exec", "");
                //text = Regex.Replace(text, " <", "");
                //text = Regex.Replace(text, ">", "");
            }
            return text;
        } 

Copyright © 2005 - 2016 flyso.cn. 飞搜 版权所有 鄂ICP备11002783号-3