Validation detected dangerous client input - post from TinyMCE in ASP.NET

后端 未结 5 1260
礼貌的吻别
礼貌的吻别 2021-01-18 08:15

I get this error when I post from TinyMCE in an ASP.NET MVC view.

Error:

Request Validation has detected a potentially dangerous clien

5条回答
  •  别那么骄傲
    2021-01-18 08:58

    Use the decorator [ValidateInput(false)].

    You will then want to write a HTMLEncode method to make it safe.

    Let me know if you want me to post the one I use.

    Added the Encode I use

        public static class StringHelpers
    {
        public static string HtmlEncode(this string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                value = value.Replace("<", "<");
                value = value.Replace(">", ">");
                value = value.Replace("'", "'");
                value = value.Replace(@"""", """);
            }
            return value;
        }
    
        public static string HtmlDecode(this string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                value = value.Replace("<", "<");
                value = value.Replace(">", ">");
                value = value.Replace("'", "'");
                value = value.Replace(""", @"""");
            }
    
            return value;
        }
    }
    

提交回复
热议问题