Dot-Net-4.0

為什麼即使 ValidateRequest = false 也會出現潛在危險的請求錯誤

  • July 8, 2019

這是我的 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,我也會收到此錯誤?

在 .net framework 4.0 中,您必須 <httpRuntime requestValidationMode="2.0"/>在 web.config 中設置標記。

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

查看參考文章 - ASP.NET 4 Breaking Changes #1: requestValidationMode 導致 ValidateRequest=False 失敗

您只需要在您的網路配置中添加以下程式碼

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

但是請注意這樣做,您可能會成為跨站點腳本攻擊的受害者

引用自:https://stackoverflow.com/questions/8997019