.net 4.0 ValidateRequest="false" 無效

昨天安裝了VisualStudio 2010 Ultimate,今天把最近的一個項目升級到了4.0下,結果跑了一下,發現關於頁面啓用 ValidateRequest="false" 的部份失效。因而把web站點的版本及項目版本都降回原來的版本後,錯誤就消失了,因而搜索了一下,找到以下資料:web

A potentially dangerous Request.Form value was detected from the client (ctl00$MainContent$txtCode="<code></code>").promise

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. To allow pages to override application request validation settings,  set requestValidationMode="2.0" in the configuration section. After setting this value, you can then 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. For more information, see http://go.microsoft.com/fwlink/?LinkId=153133. 

Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (ctl00$MainContent$txtCode="<code></code>").

Version Information: Microsoft .NET Framework Version:4.0.21006; ASP.NET Version:4.0.21006.1安全

在安裝了Visual Studio 2010 Beta2以後,當頁面輸入框默認狀況下輸入「<」或者「>」的時候。按照訪問策略,這將致使一些安全問題,諸如:跨站腳本攻擊(cross-site scripting attack)。而這個問題的更準確描述則是,當你在安裝了.NET Framework 4.0以上版本後,當你的應用程序以.NET Framework 4.0爲框架版本,你的任意服務器請求,都將被進行服務器請求驗證(ValidationRequest),這不只包括ASP.NET,同時也包括Web Services等各類HTTP請求,不單單針對aspx頁面,也針對HTTP Handler,HTTP Module等,由於這個驗證(Valify)的過程,將會發生在BeginRequest事件以前。服務器

基於以上原理,在ASP.NET以前的版本中,請求驗證也是默認開通的,可是發生在頁面級(aspx)的,而且只在請求執行的時候生效,所以,在舊的版本中,咱們只須要按如下方式配置便可:app

在頁面級別(aspx中)設置
ValidateRequest="false"
或者
在全局級別(Web.config中)設置
<configuration>
    <system.web>
        <pages validateRequest="false">框架

可是,以上設置僅對ASP.NET4.0以上有效。在ASP.NET4.0版本上,咱們須要更多一行的配置:ide

在全局級別(Web.config中)設置
<configuration>
    <system.web>
        <httpRuntime requestValidationMode="2.0">this

這一點其實在發生錯誤的頁面中已經有說明了。在實際使用過程當中,不只如此,並且我發現requestValidationMode只要設置成小於4.0就能夠,好比:1.0,2.0,3.0,3.9都是能夠的,錯誤提示中指明用2.0,目的只是說明用ASP.NET 2.0的默認方式進行工做。spa

相關文章
相關標籤/搜索