【问题标题】:A potentially dangerous Request.Form一个潜在危险的 Request.Form
【发布时间】:2011-06-21 07:29:17
【问题描述】:

有人知道我为什么会收到以下错误吗?我已启用调试。

Server Error in '/' Application.
--------------------------------------------------------------------------------

A potentially dangerous Request.Form value was detected from the client (strContent="<p>
test</p>
"). 
Description: Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. You can disable request validation by setting validateRequest=false in the Page directive or in the configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case. 

Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (strContent="<p>
test</p>
").

Source Error: 

The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

  <%@ Page Language="C#" Debug="true" %>

or:

2) Add the following section to the configuration file of your application:

<configuration>
   <system.web>
       <compilation debug="true"/>
   </system.web>
</configuration>

Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.  

Stack Trace: 


[HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (strContent="<p>
    test</p>
").]
   System.Web.HttpRequest.ValidateString(String s, String valueName, String collectionName) +8725306
   System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, String collectionName) +111
   System.Web.HttpRequest.get_Form() +129
   System.Web.HttpRequest.get_HasForm() +8725415
   System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) +97
   System.Web.UI.Page.DeterminePostBackMode() +63
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
   System.Web.UI.Page.ProcessRequest() +80
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
   System.Web.UI.Page.ProcessRequest(HttpContext context) +49
   ASP.ajax_create_new_page_aspx.ProcessRequest(HttpContext context) +37
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3615; ASP.NET Version:2.0.50727.3618

【问题讨论】:

标签: .net asp.net vb.net jquery


【解决方案1】:

帖子包含 HTML 元素(在您的情况下为 &lt;p&gt; 标记) - 这可能表示 cross site scripting attack,这就是 asp.net 默认不允许它的原因。

您应该在提交之前进行 HTML 编码(最佳实践),或者禁用警告并可能将自己暴露给 XSS。

【讨论】:

  • HTML 帖子的目的是,我如何使用 jQuery 编码为 HTML?
  • @oshirowanen - 查看这个 SO 问题和答案:stackoverflow.com/questions/1219860/…
  • @Oded 嗨。我想问你关于你的答案。禁用asp.net checks.of(潜在危险的请求......“)是不好的做法。但在这里例如(在SO中)我希望用户能够提交

    - 所以必须有ajs html 编码的机制.....你有什么好的库吗?

  • @RoyiNamir - 现在不是一个好的库,但这样做的一般方法是允许该特定字段的 HTML 内容(我相信 .NET 4.0 或可能 4.5 有一个属性可以让您在特定字段上执行此操作)并仅将您想要的标签列入白名单(因此验证将拒绝任何 HTML 而不是 &lt;p&gt;
  • @Oded 嗨。我在这里与一些人进行了讨论并想分享他们的想法......chat.stackoverflow.com/rooms/17056/… 请告诉我你的想法 - 你的意见对我很重要。
【解决方案2】:

在 web.config 文件的标签中,插入属性为 requestValidationMode="2.0" 的 httpRuntime 元素。还要在 pages 元素中添加 validateRequest="false" 属性。

<configuration>
  <system.web>
   <httpRuntime requestValidationMode="2.0" />
   <pages validateRequest="false" />
  </system.web>
</configuration>

【讨论】:

    【解决方案3】:

    这是因为您的 POST 请求中有 HTML 标签。要允许它,您需要在 @Page 指令中设置 ValidateRequest= false。但请记住,这可能会使您的网站遭受跨站脚本攻击。

    【讨论】:

      【解决方案4】:

      在您的模型中放置[AllowHtml] 属性。

      【讨论】:

        【解决方案5】:

        我有 formdata 的 ajax 请求,所以它已经工作了 在从请求中检索数据之前使用 unvalidated 关键字。 因此,您可以在这里使用 tinymce 文本数据尝试这种方式,您也不需要修改您的 Web 配置文件。 我的代码如下:

        var data=Request.Unvalidated.Form["Key_word"];
        

        【讨论】:

          【解决方案6】:

          确保你正在改变 在实际的 Web.config 中。我在 Web.debug.config 和 Web.release.config 文件中对其进行了更改,但它不起作用。

          【讨论】:

            【解决方案7】:

            我不得不在我的web.config 文件中寻找一些东西,特别是在system.web xml 部分中,以找到可以更新&lt;pages&gt; 指令的位置......正如你所指出的。一旦我将validateReqest = "false" 属性添加到web.config 文件中的pages 指令,它就使一切都变得完整了。

            在我的特殊情况下,它不在生产服务器上,但这也不是“生产”级代码。这是一个私人本地服务器,我只作为环境中的唯一用户,所以这让我对更新该设置感觉更好。如下:

            <system.web>
                <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" validateRequest="false" />
            </system.web>
            

            【讨论】:

              【解决方案8】:

              如果这是一个 MVC 应用程序,您可以在 Controller Action 级别应用此属性以忽略输入验证:

              [验证输入(假)]

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2011-11-09
                • 2011-07-23
                • 1970-01-01
                • 2014-02-07
                • 2010-12-25
                • 1970-01-01
                • 2011-03-30
                相关资源
                最近更新 更多