目前,我们网站那里有个留言板,如何将用户输入的危险字符,转换下!请教高手!我们网站是ASP.NET开发的!

解决方案 »

  1.   

    1。 采用HTML编码保存,显示时再解码
    2。 用字典保存要替换的危险字符,保存/显示前做字符替换处理
      

  2.   

    什么危险字符,如果是防止XSS (Cross Site Scripting)的话,可以使用Microsoft Anti-Cross Site Scripting Library V4.2:http://www.microsoft.com/en-us/download/details.aspx?id=28589使用方法参考(例子使用的版本比较老,不过使用方式应该差不多。):
    Microsoft Anti-Cross Site Scripting Library V1.5: Protecting the Contoso Book Page
    http://msdn.microsoft.com/en-us/library/aa973813.aspx
      

  3.   

    /// <summary> 
        ///这个方法确保用户的输入不是恶毒的 
        /// </summary> 
        /// <param name="text">输入字符串</param> 
        /// <param name="maxLength">最大长度</param> 
        /// <returns>转换后的字符串</returns> 
        public static string InputText(string text, int maxLength)
        {
            text = text.Trim();
            if (string.IsNullOrEmpty(text))
                return string.Empty;
            if (text.Length > maxLength)
                text = text.Substring(0, maxLength);
            text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces 
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br> 
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // 
            text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags 
            text = text.Replace("'", "''");
            return text;
        }
      

  4.   

    你说的危险字符是不是脚本攻击?
    类似:<script></script>?
    可用下面的正则将其去除
    Regex.Replace(str, "<script[^>]*?>.*?</script>", "");
      

  5.   


    参数化提交只是在sql注入方面有用  xss现在还不能做到100%防御吧 只能过滤掉大部分有害字符 增加被入侵难度
      

  6.   

    你的意思是留言板内容是马上公开的?
    那参数化后,再将<转换成&lt;>转换成&gt;基本就可以了!