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

后端 未结 5 1263
礼貌的吻别
礼貌的吻别 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("<", "&lt;");
                value = value.Replace(">", "&gt;");
                value = value.Replace("'", "&apos;");
                value = value.Replace(@"""", "&quot;");
            }
            return value;
        }
    
        public static string HtmlDecode(this string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                value = value.Replace("&lt;", "<");
                value = value.Replace("&gt;", ">");
                value = value.Replace("&apos;", "'");
                value = value.Replace("&quot;", @"""");
            }
    
            return value;
        }
    }
    
    0 讨论(0)
  • 2021-01-18 09:01

    Try this solution. simply add to TinyMce control

    tinyMCE.init({
    ...
    encoding : "xml"
    });
    

    http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/encoding

    http://blog.tentaclesoftware.com/archive/2010/07/22/96.aspx

    0 讨论(0)
  • 2021-01-18 09:05

    Annoyingly in version 4 of tinymce they seem to have removed the encoding: xml option.

    I ended up using a javascript HTML encoding function from this answer, and on my submit button I encode the contents of the textarea before the form submits, by using tinymce's getContent and setContent methods

    0 讨论(0)
  • 2021-01-18 09:16

    Try using the [AllowHtml] attribute in your model.

    class MyModel{
     [AllowHtml]
     public string Content{get;set;}
    }
    
    0 讨论(0)
  • 2021-01-18 09:18

    I had the same problem. I didn't want to disable ASP.NET MVC validation feature, so I kept looking until I reached this solution:

    At the tinyMCE plugin code encode your content (I'm using the older version)

    tinyMCE.init({
       ...
       encoding: "xml"
    });
    

    And after this I didn't get any more the application validation error. Then I came up with another problem when I edited my form the code would come up with the html tags

    <strong>My input value</strong>
    

    instead of this

    My input value

    So, I had to decode the html for that field when getting my values at the Controller, like this:

    ...    
    entity.field = HttpUtility.HtmlDecode(entity.field);
    
    0 讨论(0)
提交回复
热议问题