Validation detected dangerous client input - post

2020-04-05 08:18发布

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

Error:

Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted

From googling, it says to just add a validateRequest in the Page directive at the top which I did, but I STILL get this error. As you can see, below is my code in the view:

<%@ Page validateRequest="false" Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

5条回答
手持菜刀,她持情操
2楼-- · 2020-04-05 08:58

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

查看更多
We Are One
3楼-- · 2020-04-05 08:59

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;
    }
}
查看更多
我命由我不由天
4楼-- · 2020-04-05 09:15

Try using the [AllowHtml] attribute in your model.

class MyModel{
 [AllowHtml]
 public string Content{get;set;}
}
查看更多
▲ chillily
5楼-- · 2020-04-05 09:20

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

查看更多
一纸荒年 Trace。
6楼-- · 2020-04-05 09:21

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);
查看更多
登录 后发表回答