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

前端 未结 30 2216
刺人心
刺人心 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:40

    Cause

    ASP.NET by default validates all input controls for potentially unsafe contents that can lead to cross-site scripting (XSS) and SQL injections. Thus it disallows such content by throwing the above exception. By default it is recommended to allow this check to happen on each postback.

    Solution

    On many occasions you need to submit HTML content to your page through Rich TextBoxes or Rich Text Editors. In that case you can avoid this exception by setting the ValidateRequest tag in the @Page directive to false.

    <%@ Page Language="C#" AutoEventWireup="true" ValidateRequest = "false" %>
    

    This will disable the validation of requests for the page you have set the ValidateRequest flag to false. If you want to disable this, check throughout your web application; you’ll need to set it to false in your web.config <system.web> section

    <pages validateRequest ="false" />
    

    For .NET 4.0 or higher frameworks you will need to also add the following line in the <system.web> section to make the above work.

    <httpRuntime requestValidationMode = "2.0" />
    

    That’s it. I hope this helps you in getting rid of the above issue.

    Reference by: ASP.Net Error: A potentially dangerous Request.Form value was detected from the client

    0 讨论(0)
  • 2020-11-21 05:42

    For ASP.NET 4.0, you can allow markup as input for specific pages instead of the whole site by putting it all in a <location> element. This will make sure all your other pages are safe. You do NOT need to put ValidateRequest="false" in your .aspx page.

    <configuration>
    ...
      <location path="MyFolder/.aspx">
        <system.web>
          <pages validateRequest="false" />
          <httpRuntime requestValidationMode="2.0" />
        </system.web>
      </location>
    ...
    </configuration>
    

    It is safer to control this inside your web.config, because you can see at a site level which pages allow markup as input.

    You still need to programmatically validate input on pages where request validation is disabled.

    0 讨论(0)
  • 2020-11-21 05:43

    The answer to this question is simple:

    var varname = Request.Unvalidated["parameter_name"];
    

    This would disable validation for the particular request.

    0 讨论(0)
  • 2020-11-21 05:43

    In ASP.NET, you can catch the exception and do something about it, such as displaying a friendly message or redirect to another page... Also there is a possibility that you can handle the validation by yourself...

    Display friendly message:

    protected override void OnError(EventArgs e)
    {
        base.OnError(e);
        var ex = Server.GetLastError().GetBaseException();
        if (ex is System.Web.HttpRequestValidationException)
        {
            Response.Clear();
            Response.Write("Invalid characters."); //  Response.Write(HttpUtility.HtmlEncode(ex.Message));
            Response.StatusCode = 200;
            Response.End();
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:46

    For MVC, ignore input validation by adding

    [ValidateInput(false)]

    above each Action in the Controller.

    0 讨论(0)
提交回复
热议问题