0

I have a simple email form on my site with mvc c#.

If i added into the text box alert("test") I get the below exception:

A potentially dangerous Request.Form value was detected from the client (Message="<script>alert("test"...").

I dont want a user to be able to insert javascript. I need for html encode i would i do this on this field

 @Html.TextAreaFor(model => model.Message, new { @style = "width:800px;height:300px;" })
tereško
  • 58,060
  • 25
  • 98
  • 150
Matthew Chambers
  • 869
  • 3
  • 20
  • 34
  • possible duplicate of [A potentially dangerous Request.Form value was detected](http://stackoverflow.com/questions/5937127/a-potentially-dangerous-request-form-value-was-detected) – Peter Feb 05 '15 at 07:54

2 Answers2

1

Option 1: look at the accepted answer at: HTML-encoding lost when attribute read from input field

Option 2: Put the [AllowHtml] attribute on the model item that binds to this textbox and that will let the value into your controller where you can use HtmlEncode.

Option 3: Put the [ValidateInput(false)] attribute on your controller action, this lets everything through no matter what and then you can do your own custom validation for everything

Community
  • 1
  • 1
krilovich
  • 3,475
  • 1
  • 23
  • 33
0

Use System.Web.HttpUtility.HtmlEncode to encode all user input and avoid XSS atacks.

Oscar
  • 13,594
  • 8
  • 47
  • 75