http://blog.sina.com.cn/caicaiblog110[订阅]
字体大小: 正文
asp.net 防字符串注入方法(2008-08-11 11:05:03)
CODE:

#region 替换特殊字符
        /// <summary>
        /// 特殊字符串替换
        /// </summary>
        public static string repString(string strTemp)
        {
            if (strTemp == null)
                strTemp = "";
            strTemp = strTemp.Replace(" ", "");
            strTemp = strTemp.Replace("*", "");
            strTemp = strTemp.Replace("?", "");
            strTemp = strTemp.Replace("#", "");
            strTemp = strTemp.Replace("@", "");
            strTemp = strTemp.Replace("^", "");
            strTemp = strTemp.Replace("&", "");
            strTemp = strTemp.Replace("+", "");
            strTemp = strTemp.Replace("-", "");
            strTemp = strTemp.Replace("(", "");
            strTemp = strTemp.Replace(")", "");
            strTemp = strTemp.Replace("!", "");
            strTemp = strTemp.Replace("`", "");
            strTemp = strTemp.Replace("~", "");
            strTemp = strTemp.Replace("<", "");
            strTemp = strTemp.Replace(">", "");
            strTemp = strTemp.Replace("'", "");
            strTemp = strTemp.Replace("\"", "");
            strTemp = strTemp.Replace("\\", "");
            strTemp = strTemp.Replace("|", "");
            strTemp = strTemp.Replace("=", "");
            strTemp = strTemp.Replace(",", "");
            return strTemp;
        }
        #endregion


#region 删除html格式
        /// <summary>
        /// 替换html特殊字符
        /// </summary>
        /// <param name="strContent"></param>
        /// <returns></returns>
        public static string repHtml(string strContent)
        {
            strContent = strContent.Replace("&", "&amp");
            strContent = strContent.Replace("´", "´´");
            strContent = strContent.Replace("<", "&lt");
            strContent = strContent.Replace(">", "&gt");
            strContent = strContent.Replace("chr(60)", "&lt");
            strContent = strContent.Replace("chr(37)", "&gt");
            strContent = strContent.Replace("\"", "&quot");
            strContent = strContent.Replace(";", ";");
            strContent = strContent.Replace("\n", "<br />");
            strContent = strContent.Replace(" ", "&nbsp");
            return strContent;
        }
        /// <summary>
        /// 清除html特殊字符
        /// </summary>
        /// <param name="strContent"></param>
        /// <returns></returns>
        public static string clearHtml(string strContent)
        {
            strContent = strContent.Replace("&", "");
            strContent = strContent.Replace("´", "");
            strContent = strContent.Replace("<", "");
            strContent = strContent.Replace(">", "");
            strContent = strContent.Replace("chr(60)", "");
            strContent = strContent.Replace("chr(37)", "");
            strContent = strContent.Replace("\"", "");
            strContent = strContent.Replace(";", "");
            strContent = strContent.Replace("\n", "<br/>");
            strContent = strContent.Replace("\\", "");
            return strContent;
        }
        #endregion
  • 评论加载中,请稍候...
发评论    明星私家相册

验证码:看不清楚数字吗?点击这里再试试。收听验证码

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

相关博文
读取中...
推荐博文
读取中...