为什么有潜在危险的请求错误甚至ValidateRequest = false

时间:2012-01-25 02:03:23

标签: .net-4.0 asp.net-4.0

这是我的Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
<html>
    <head runat="server">
        <title>xss demonstration</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            We are looking for your feedback. 
            <asp:TextBox ID="txtFeedback" runat="server" TextMode="MultiLine" />
            <br />
            <asp:Button ID="submit" runat="server" Text="Submit" onclick="submit_Click" />
            <br />
            Comment: 
            <br />
            <asp:Literal ID="ltlFeedback" runat="server" />
        </div>
        </form>
    </body>
</html>

以下是Default.aspx.cs

public partial class _Default : System.Web.UI.Page 
{
    protected void submit_Click(object sender, EventArgs e)
    {
        this.ltlFeedback.Text = this.txtFeedback.Text;
    }
}

当我运行应用程序并在文本框中输入以下内容时。

<script>alert('Hello')</script>

我收到以下错误。

  

从中检测到一个潜在危险的Request.Form值   客户端(txtFeedback =“alert('Hello ...”)。

我的问题是,即使在页面中将ValidateRequest设置为false,我也会收到此错误?

3 个答案:

答案 0 :(得分:12)

在.net framework 4.0中,您必须在web.config中设置<httpRuntime requestValidationMode="2.0"/>标记。

<system.web>
     <compilation debug="false" targetFramework="4.0" />
     <httpRuntime requestValidationMode="2.0"/>
</system.web>

查看参考文章 - ASP.NET 4 Breaking Changes #1: requestValidationMode cause ValidateRequest=False to fail

答案 1 :(得分:0)

您只需在网络配置中添加以下代码

即可
   <system.web>
         <compilation debug="false" targetFramework="4.0" />
         <httpRuntime requestValidationMode="2.0"/>
 </system.web>

但要注意这样做,你可能成为跨站点脚本攻击的受害者

答案 2 :(得分:0)

在通过Server.Execute检索的任何页面上禁用ValidateRequest

我最近遇到了这个错误的深奥版本。为了在不同站点中的页面之间包含共享内容,我在另一个ASPX页面上调用Server.Execute。默认情况下,Server.Execute会将POST数据转发到子请求,因此此页面 也需要ValidateRequest="false"

只要设置了<httpRuntime requestValidationMode="2.0"/>中的web.config,则异常的堆栈跟踪将指示哪个页面类引发了错误。

相关问题