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

前端 未结 30 2856
刺人心
刺人心 2020-11-21 05:24

Every time a user posts something containing < or > in a page in my web application, I get this exception thrown.

I don\'t want to go

30条回答
  •  野性不改
    2020-11-21 05:46

    The previous answers are great, but nobody said how to exclude a single field from being validated for HTML/JavaScript injections. I don't know about previous versions, but in MVC3 Beta you can do this:

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

    This still validates all the fields except for the excluded one. The nice thing about this is that your validation attributes still validate the field, but you just don't get the "A potentially dangerous Request.Form value was detected from the client" exceptions.

    I've used this for validating a regular expression. I've made my own ValidationAttribute to see if the regular expression is valid or not. As regular expressions can contain something that looks like a script I applied the above code - the regular expression is still being checked if it's valid or not, but not if it contains scripts or HTML.

提交回复
热议问题