• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

Avoiding the ‘A potentially dangerous Request.Form value was detected’

January 28, 2013 By _tasos 3 Comments

This is a common error that ASP.NET developers have run into many times. We will see in this article a few ways on how to avoid it, both in Web Forms and MVC.

This error occurs mostly because the data that are sent in the web server contain HTML. By default there a validation check on all input so that our web application has a basic protection from XSS attacks.
The easy way is to disable this validation process. This can be done by setting the below properties in the Web.config file.

<configuration> 
  <system.web> 
    <pages validateRequest="false" /> 
  </system.web> 
</configuration>

In .NET 4.0, you need to change one more property.

<configuration> 
  <system.web> 
    <pages validateRequest="false" />  
    <httpRuntime requestValidationMode="2.0"/>
  </system.web> 
</configuration>

If you want to apply this setting to a specific page, you can set it in the page directive of your .aspx file.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Example.aspx.cs" 
                    Inherits="Example.Example" ValidateRequest="false" %>

In case your web application is an ASP.NET MVC project and want to disable page validation for a specific view, you can set this property to its controller.

[Post, ValidateInput(false)]
public ActionResult Edit(...) {
    ...
}

In MVC, you can actually declare which properties of your model you want to exclude from the validation process.

[HttpPost, ValidateInput(true, Exclude = "YourFieldName")]
public virtual ActionResult Edit(...)
{
    ...
}

Another approach is to set at the property of the Model to allow HTML content.

[AllowHtml]
public string Description { get; set; }

Going back to Web Forms, if you want to allow HTML / JavaScript for a specific field, there is no direct way to do it. A nice trick you could do, is to encode the HTML in the client side and then decode it in the server side.
The client side code

// The event to escape the data and store in our HiddenField
jQuery('.allow_html textarea').blur(function () {
    jQuery(jQuery(this).parent()).find('input[type="hidden"]').val(escape(jQuery(this).val()));
});
 
// The code to unescape the code and set it in our textbox
jQuery('.allow_html textarea').each(function(idx, item) {
    var value = jQuery(jQuery(item).parent()).find('input[type="hidden"]').val();
    jQuery(item).val(unescape(value));
});

The server side code

// encode the data
HtmlCodeHiddenField.Value = Uri.EscapeDataString(EscapedHtml);
// decode the data
string myHtml = Uri.UnescapeDataString(HtmlCodeHiddenField.Value);

If you want to read more about this subject, read this article from Stack Overflow.

Filed Under: ASP.NET, IIS Tagged With: ASP.NET MVC, ASP.NET Web Forms

Reader Interactions

Comments

  1. Kuido Külm says

    March 26, 2013 at 19:57

    From another side setting pages validateRequest=”false” opens your web page to Cross Site Injection (XSS) attacks. Use Server.HtmlEncode or some AntiXSS libarary functions

    Reply
  2. Spidergeuse says

    August 26, 2014 at 13:44

    Good. I prefer to just escape and unescape on client side

    Reply
  3. Jordan says

    February 15, 2018 at 19:57

    I got the error when a user had a password containing an ampersand and a pound sign side-by-side. But that’s not HTML.

    Reply

Leave a Reply to Jordan Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Geekiness Intensifies.. NASA used Three.js to render a real-time simulation of this week's NASA rover landing on M… https://t.co/orgkXnYj9O February 19, 2021 18:12
  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in