Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET
Tip/Trick

ASP.NET 4.0 potentially dangerous Request.Form value was detected

Rate me:
Please Sign up or sign in to vote.
4.88/5 (50 votes)
6 Feb 2013CPOL1 min read 580K   27   56
If anyone enters non-encoded HTML content into a text box in an ASP.NET application.

A few days ago, while working on an ASP.NET 4.0 Web project, I got an issue. The issue was, when user enters non-encoded HTML content into a comment text box s/he got something like the following error message:

"A potentially dangerous Request.Form value was detected from the client".

This was because .NET detected something in the entered text which looked like an HTML statement. Then I got a link Request Validation, that is a feature put in place to protect your application cross site scripting attack and followed accordingly.

To disable request validation, I added the following to the existing "page" directive in that .aspx file.

ValidateRequest="false"

But I still got the same error. Later I found that, for .NET 4, we need to add requestValidationMode="2.0" to the httpRuntime configuration section of the web.config file like the following:

XML
<httpRuntime requestValidationMode="2.0"/>

But if there is no httpRuntime section in the web.config file, then this goes inside the <system.web> section.

If anyone wants to turn off request validation globally for a user, the following line in the web.config file within <system.web> section will help:

XML
<pages validateRequest="false" />  

Note: But always avoid the last example because there is a huge security issue. The request validation feature in ASP.NET provides a certain level of default protection against cross-site scripting (XSS) attacks.

However, we recommend that you analyze any request validation errors to determine whether existing handlers, modules, or other custom code accesses potentially unsafe HTTP inputs that could be XSS attack vectors.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
A life-long-learner, maker and soft music fan. Likes building things to solve problems. Years of successful records serving mid and large scale .NET applications in domestic and international client environment. Expertise in different areas of software development life cycles and Software Architecture.

Always looks for new technology and loves to get hands dirty Smile | :)

Comments and Discussions

 
AnswerRe: hi Pin
Monjurul Habib28-Jun-12 20:13
professionalMonjurul Habib28-Jun-12 20:13 
GeneralMy vote of 5 Pin
walkerwzy22-Jun-12 21:16
walkerwzy22-Jun-12 21:16 
GeneralRe: My vote of 5 Pin
Monjurul Habib24-Jun-12 21:37
professionalMonjurul Habib24-Jun-12 21:37 
GeneralRealy Helpfull Pin
Arman Erfani11-Jun-12 21:34
Arman Erfani11-Jun-12 21:34 
GeneralRe: Realy Helpfull Pin
Monjurul Habib14-Jun-12 10:05
professionalMonjurul Habib14-Jun-12 10:05 
GeneralMy vote of 5 Pin
techchallenger6-May-12 14:23
techchallenger6-May-12 14:23 
GeneralRe: My vote of 5 Pin
Monjurul Habib7-May-12 21:17
professionalMonjurul Habib7-May-12 21:17 
GeneralReason for my vote of 5 Exactly my scenario, and was helpful Pin
Member 467175418-Feb-12 9:33
Member 467175418-Feb-12 9:33 
Reason for my vote of 5
Exactly my scenario, and was helpful
GeneralRe: thank you Pin
Monjurul Habib25-Feb-12 23:52
professionalMonjurul Habib25-Feb-12 23:52 
GeneralReason for my vote of 5 excellent post Pin
jpmontoya18226-Dec-11 7:43
jpmontoya18226-Dec-11 7:43 
GeneralRe: thank you Pin
Monjurul Habib26-Dec-11 8:25
professionalMonjurul Habib26-Dec-11 8:25 
GeneralReason for my vote of 5 Run into this several times recently... Pin
PeterA20-Dec-11 10:14
professionalPeterA20-Dec-11 10:14 
GeneralRe: thank you Pin
Monjurul Habib20-Dec-11 19:28
professionalMonjurul Habib20-Dec-11 19:28 
GeneralThis does not address the security aspect of setting Validat... Pin
davecal20-Dec-11 3:47
davecal20-Dec-11 3:47 
GeneralRe: This is merely another example of microsoft trying to 'save'... Pin
Verrice22-Dec-11 6:10
Verrice22-Dec-11 6:10 
GeneralReason for my vote of 3 While this is a workaround it doesn'... Pin
davecal20-Dec-11 3:45
davecal20-Dec-11 3:45 
GeneralThis will be easier with ASP.NET 4.5: http://www.asp.net/vne... Pin
Richard Deeming15-Dec-11 8:41
mveRichard Deeming15-Dec-11 8:41 
GeneralRe: I was talking about ASP.NET 4.0, anyway thank you Pin
Monjurul Habib18-Dec-11 16:48
professionalMonjurul Habib18-Dec-11 16:48 
GeneralReason for my vote of 5 Excellent. Needs a link or an extent... Pin
Dave Vroman14-Dec-11 5:23
professionalDave Vroman14-Dec-11 5:23 
GeneralRe: thank you Pin
Monjurul Habib14-Dec-11 6:46
professionalMonjurul Habib14-Dec-11 6:46 
GeneralReason for my vote of 5 Great this help solve my problem of ... Pin
Diing13-Dec-11 17:22
Diing13-Dec-11 17:22 
GeneralRe: thank you Pin
Monjurul Habib13-Dec-11 18:46
professionalMonjurul Habib13-Dec-11 18:46 
GeneralReason for my vote of 5 keep it up! Pin
Aftab Quraishi12-Dec-11 19:06
Aftab Quraishi12-Dec-11 19:06 
GeneralRe: thanks boss :) Pin
Monjurul Habib12-Dec-11 19:13
professionalMonjurul Habib12-Dec-11 19:13 
GeneralReason for my vote of 5 useful Pin
Humayun Kabir Mamun10-Dec-11 22:53
Humayun Kabir Mamun10-Dec-11 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.