asp.net过滤不良字符(今天用到了这个东西上网搜了搜总结一下)

net过滤非法字符

2010-05-13 21:22

/**//// <summary>
        /// 过滤sql中非法字符
         /// </summary>
        /// <param name="value">要过滤的字符串 </param>
        /// <returns>string </returns>
        public static string Filter(string value)
        {
            if (string.IsNullOrEmpty(value)) return string.Empty;
            value = Regex.Replace(value, @";", string.Empty);
            value = Regex.Replace(value, @"'", string.Empty);
            value = Regex.Replace(value, @"&", string.Empty);
            value = Regex.Replace(value, @"%20", string.Empty);
            value = Regex.Replace(value, @"--", string.Empty);
            value = Regex.Replace(value, @"==", string.Empty);
            value = Regex.Replace(value, @" <", string.Empty);
            value = Regex.Replace(value, @">", string.Empty);
            value = Regex.Replace(value, @"%", string.Empty);

            return value;
        }

 

/// <summary>
        /// 过滤非法字符
        /// </summary>
        /// <param name="inputString1">等待处理的字符串</param>
        /// <returns></returns>
        public static string InputText3(object inputString1)
        {
            if (inputString1 == null) return "";
            string inputString = inputString1.ToString();
            StringBuilder retVal = new StringBuilder();
            if (inputString != String.Empty)
            {
                inputString = inputString.Trim().ToLower();
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '"':
                            retVal.Append("");
                            break;
                        case '<':
                            retVal.Append("");
                            break;
                        case '>':
                            retVal.Append("");
                            break;

                        default:
                            retVal.Append(inputString[i]);
                            break;
                    }
                }
                retVal = retVal.Replace("'", "");
                retVal = retVal.Replace(";", "");
                retVal = retVal.Replace(":", "");
            }
          
            return retVal.ToString();
        }

/// <summary>
        /// 过滤非法字符,判断字符串
        /// </summary>
        /// <param name="inputString1">等待处理的字符串</param>
        /// <returns></returns>
        public static string InputText2(object inputString1)
        {
            if (inputString1 == null || inputString1.ToString() == "") return "";
            string inputString = inputString1.ToString();
            StringBuilder retVal = new StringBuilder();
            if (inputString != String.Empty)
            {
                inputString = inputString.Trim().ToLower();
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '"':
                            retVal.Append("");
                            break;
                        case '<':
                            retVal.Append("");
                            break;
                        case '>':
                            retVal.Append("");
                            break;

                        default:
                            retVal.Append(inputString[i]);
                            break;
                    }
                }
                retVal = retVal.Replace("'", "");
                retVal = retVal.Replace(";", "");
                retVal = retVal.Replace(":", "");
            
               
            }
            if (!IsInt(retVal.ToString()))
            {
                HttpContext.Current.Response.Redirect(SiteCommon.SiteConst.URL_ERROR_PAGE);
            }
            return retVal.ToString();
        }

   /// <summary>
        /// 是否整数
        /// </summary>
        /// <param name="str1">原字符</param>
        /// <returns></returns>
        public static bool IsInt(string str1)
        {
            if (string.IsNullOrEmpty(str1))
            {
                return false;
            }
            Regex reg = new Regex("^[0-9]+[0-9]*$");
            Match match = reg.Match(str1);
            return match.Success;
        }

 

 

/**//// <summary>
        /// 检测输入字符串strInput是否在禁用字符串strConfigWords中存在,存在则过滤该输入值。
        /// </summary>
        public string CheckRepWords(string strConfigWords,string strInput)
        {
            //创建strWords数组,以“|”号为分割符。
            string strWords = strConfigWords.Split(new char {'|'});
            //依此读取数组中各个单元,并检测该字符串是否存在于以定义过滤词组strConfigWords中,
            //strConfigWords一行内的格式为:“str1,str2”,若str1存在于输入内容strInput内,
            //则将str1替换为str2。
            foreach(string strWord in strWords)
            {
                string strSplitWord = strWord.Split(new char {','});
                if(strInput.IndexOf(strSplitWord[0])>=0)
                {
                    strInput = strInput.Replace(strSplitWord[0],strSplitWord);
                }
            }
            return strInput;
        }


net过滤非法字符

2010-05-13 21:22

/**//// <summary>
        /// 过滤sql中非法字符
         /// </summary>
        /// <param name="value">要过滤的字符串 </param>
        /// <returns>string </returns>
        public static string Filter(string value)
        {
            if (string.IsNullOrEmpty(value)) return string.Empty;
            value = Regex.Replace(value, @";", string.Empty);
            value = Regex.Replace(value, @"'", string.Empty);
            value = Regex.Replace(value, @"&", string.Empty);
            value = Regex.Replace(value, @"%20", string.Empty);
            value = Regex.Replace(value, @"--", string.Empty);
            value = Regex.Replace(value, @"==", string.Empty);
            value = Regex.Replace(value, @" <", string.Empty);
            value = Regex.Replace(value, @">", string.Empty);
            value = Regex.Replace(value, @"%", string.Empty);

            return value;
        }

 

/// <summary>
        /// 过滤非法字符
        /// </summary>
        /// <param name="inputString1">等待处理的字符串</param>
        /// <returns></returns>
        public static string InputText3(object inputString1)
        {
            if (inputString1 == null) return "";
            string inputString = inputString1.ToString();
            StringBuilder retVal = new StringBuilder();
            if (inputString != String.Empty)
            {
                inputString = inputString.Trim().ToLower();
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '"':
                            retVal.Append("");
                            break;
                        case '<':
                            retVal.Append("");
                            break;
                        case '>':
                            retVal.Append("");
                            break;

                        default:
                            retVal.Append(inputString[i]);
                            break;
                    }
                }
                retVal = retVal.Replace("'", "");
                retVal = retVal.Replace(";", "");
                retVal = retVal.Replace(":", "");
            }
          
            return retVal.ToString();
        }

/// <summary>
        /// 过滤非法字符,判断字符串
        /// </summary>
        /// <param name="inputString1">等待处理的字符串</param>
        /// <returns></returns>
        public static string InputText2(object inputString1)
        {
            if (inputString1 == null || inputString1.ToString() == "") return "";
            string inputString = inputString1.ToString();
            StringBuilder retVal = new StringBuilder();
            if (inputString != String.Empty)
            {
                inputString = inputString.Trim().ToLower();
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '"':
                            retVal.Append("");
                            break;
                        case '<':
                            retVal.Append("");
                            break;
                        case '>':
                            retVal.Append("");
                            break;

                        default:
                            retVal.Append(inputString[i]);
                            break;
                    }
                }
                retVal = retVal.Replace("'", "");
                retVal = retVal.Replace(";", "");
                retVal = retVal.Replace(":", "");
            
               
            }
            if (!IsInt(retVal.ToString()))
            {
                HttpContext.Current.Response.Redirect(SiteCommon.SiteConst.URL_ERROR_PAGE);
            }
            return retVal.ToString();
        }

   /// <summary>
        /// 是否整数
        /// </summary>
        /// <param name="str1">原字符</param>
        /// <returns></returns>
        public static bool IsInt(string str1)
        {
            if (string.IsNullOrEmpty(str1))
            {
                return false;
            }
            Regex reg = new Regex("^[0-9]+[0-9]*$");
            Match match = reg.Match(str1);
            return match.Success;
        }

 

 

/**//// <summary>
        /// 检测输入字符串strInput是否在禁用字符串strConfigWords中存在,存在则过滤该输入值。
        /// </summary>
        public string CheckRepWords(string strConfigWords,string strInput)
        {
            //创建strWords数组,以“|”号为分割符。
            string strWords = strConfigWords.Split(new char {'|'});
            //依此读取数组中各个单元,并检测该字符串是否存在于以定义过滤词组strConfigWords中,
            //strConfigWords一行内的格式为:“str1,str2”,若str1存在于输入内容strInput内,
            //则将str1替换为str2。
            foreach(string strWord in strWords)
            {
                string strSplitWord = strWord.Split(new char {','});
                if(strInput.IndexOf(strSplitWord[0])>=0)
                {
                    strInput = strInput.Replace(strSplitWord[0],strSplitWord);
                }
            }
            return strInput;
        }

 

posted @ 2010-12-18 16:07  西湖小巷  阅读(2861)  评论(0编辑  收藏  举报