I'm using a rich text editor in my asp.net mvc form (nicedit with a textarea) and when I submit the form on post, because it is not html encoded I get the following message: "A potentially dangerous Request.Form value was detected from the client" . How can I html encode the textarea on post ? I don't want to cancel the validation. Is there a way to use the html.encode helper on submit?
Thank you.
Decorating the field with AllowHtml will do the job without omiting the validation logic. This solved the problem in my case without encode/decode.
Are you using .net 4.0? If so you will also need
in your config.web file.
Rather than switching off
ValidateInput
, as then you are open to vulnerabilities, you could use Javascript to encode the special charaters. This allows you to not throw the error message:for some simple inputs (such as emails in the format
MyName<me@somewhere.com>
) but still having the built in MVC function to watch your back for other script injection. Off course if you need the input in the correct format at the server you will have to decode it and be careful if you are outputting it againIf already using jQuery, this can easily be added to all input fields as follows
htmlEscape
here is my own function to change the special chars.Depending on your needs you may want to escape all characters using the built in Javascript function
encodeURI
or extend the above function such as:You could decorate the action handling the form post with the ValidateInputAttribute: