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

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

    None of the suggestions worked for me. I did not want to turn off this feature for the whole website anyhow because 99% time I do not want my users placing HTML on web forms. I just created my own work around method since I'm the only one using this particular application. I convert the input to HTML in the code behind and insert it into my database.

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

    I think you are attacking it from the wrong angle by trying to encode all posted data.

    Note that a "<" could also come from other outside sources, like a database field, a configuration, a file, a feed and so on.

    Furthermore, "<" is not inherently dangerous. It's only dangerous in a specific context: when writing strings that haven't been encoded to HTML output (because of XSS).

    In other contexts different sub-strings are dangerous, for example, if you write an user-provided URL into a link, the sub-string "javascript:" may be dangerous. The single quote character on the other hand is dangerous when interpolating strings in SQL queries, but perfectly safe if it is a part of a name submitted from a form or read from a database field.

    The bottom line is: you can't filter random input for dangerous characters, because any character may be dangerous under the right circumstances. You should encode at the point where some specific characters may become dangerous because they cross into a different sub-language where they have special meaning. When you write a string to HTML, you should encode characters that have special meaning in HTML, using Server.HtmlEncode. If you pass a string to a dynamic SQL statement, you should encode different characters (or better, let the framework do it for you by using prepared statements or the like)..

    When you are sure you HTML-encode everywhere you pass strings to HTML, then set ValidateRequest="false" in the <%@ Page ... %> directive in your .aspx file(s).

    In .NET 4 you may need to do a little more. Sometimes it's necessary to also add <httpRuntime requestValidationMode="2.0" /> to web.config (reference).

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

    If you are on .NET 4.0 make sure you add this in your web.config file inside the <system.web> tags:

    <httpRuntime requestValidationMode="2.0" />
    

    In .NET 2.0, request validation only applied to aspx requests. In .NET 4.0 this was expanded to include all requests. You can revert to only performing XSS validation when processing .aspx by specifying:

    requestValidationMode="2.0"
    

    You can disable request validate entirely by specifying:

    validateRequest="false"
    
    0 讨论(0)
  • 2020-11-21 05:39

    You can catch that error in Global.asax. I still want to validate, but show an appropriate message. On the blog listed below, a sample like this was available.

        void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
    
            if (ex is HttpRequestValidationException)
            {
                Response.Clear();
                Response.StatusCode = 200;
                Response.Write(@"[html]");
                Response.End();
            }
        }
    

    Redirecting to another page also seems like a reasonable response to the exception.

    http://www.romsteady.net/blog/2007/06/how-to-catch-httprequestvalidationexcep.html

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

    In the web.config file, within the tags, insert the httpRuntime element with the attribute requestValidationMode="2.0". Also add the validateRequest="false" attribute in the pages element.

    Example:

    <configuration>
      <system.web>
       <httpRuntime requestValidationMode="2.0" />
      </system.web>
      <pages validateRequest="false">
      </pages>
    </configuration>
    
    0 讨论(0)
  • 2020-11-21 05:39

    As long as these are only "<" and ">" (and not the double quote itself) characters and you're using them in context like <input value="this" />, you're safe (while for <textarea>this one</textarea> you would be vulnerable of course). That may simplify your situation, but for anything more use one of other posted solutions.

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