.net 4.0 为什么存在潜在危险的请求错误,甚至ValidateRequest=false

.net 4.0 为什么存在潜在危险的请求错误,甚至ValidateRequest=false,.net-4.0,asp.net-4.0,.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> <

这是我的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>
当我运行应用程序并在文本框中输入以下内容时

<script>alert('Hello')</script>
警报('Hello')
我得到以下错误

潜在危险的请求。从中检测到表单值 客户端(txtFeedback=“警报('Hello…”)


我的问题是,为什么在页面中将ValidateRequest设置为false时仍会出现此错误?

在.net framework 4.0中,您必须在web.config中设置标记

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


看看参考文章-。

您只需要在web配置中添加以下代码

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

但请注意,这样做可能会使您成为跨站点脚本攻击的受害者

在通过服务器检索的任何页面上禁用ValidateRequest。执行 我最近遇到了这个错误的一个相当深奥的版本。为了在不同站点的页面中包含共享内容,我调用了
Server。在另一个ASPX页面上执行
。默认情况下,
Server.Execute
将POST数据转发给子请求,因此此页面还需要
ValidateRequest=“false”

只要在
web.config
中设置了
,异常的堆栈跟踪将指示引发错误的页面类

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