為什么拋 System.Web.HttpRequestValidationException?
從客戶端(TextBox1="<td>")中檢測到有潛在危險的 Request.Form 值。
說明: 請求驗證過程檢測到有潛在危險的客戶端輸入值,對請求的處理已經中止。該值可能指示存在危及應用程序安全的嘗試,如跨站點腳本攻擊。若要允許頁面重寫應用程序請求驗證設置,請將 httpRuntime 配置節中的 requestValidationMode 特性設置為 requestValidationMode="2.0"。示例: <httpRuntime requestValidationMode="2.0" />。設置此值后,可通過在 Page 指令或 <pages> 配置節中設置 validateRequest="false" 禁用請求驗證。但是,在這種情況下,強烈建議應用程序顯式檢查所有輸入。有關更多信息,請參見 http://go.microsoft.com/fwlink/?LinkId=153133。
異常詳細信息: System.Web.HttpRequestValidationException: 從客戶端(TextBox1="<td>")中檢測到有潛在危險的 Request.Form 值。
其實description 已經講得很明白了,當我們試圖在文本框內輸入html標簽的時候(這里輸入的<td>),request validation的時候為了防止危險的客戶端輸入就會拋HttpRequestValidationException。
至於為什么要進行這樣的驗證呢?
下面這篇文章介紹的很好。
警告:為了安全請不要隨意將ASP.Net的validateRequest="false"
我們在這里的解決途徑有以下幾種:
1. disable validate request(不管推不推薦這是最普遍的)
在3.5及以下版本中 我們可以用如下幾種操作:
- ASP.Net頁面描述中通過設置 validateRequest=false 來禁用這個特性
- 在web.config中配置
<configuration>
<system.web>
<pages validateRequest="false" />
</system.web>
</configuration>
在4.0中我們則不能夠這樣做了,但是我們可以通過重寫RequestValidator中的IsValidRequestString 來達到同樣的效果。
首先新建一個RequestValidatorDisabled類。
using System; class RequestValidatorDisabled : System.Web.Util.RequestValidator { protected override bool IsValidRequestString(System.Web.HttpContext context, string value, System.Web.Util.RequestValidationSource requestValidationSource, string collectionKey, out int validationFailureIndex) { validationFailureIndex = -1; return true; } }
然后在webconfig system.web下加上
<httpRuntime requestValidationType="RequestValidatorDisabled" />
這樣也可以有相同的效果
2.在產生錯誤的文件的頂部添加一條“Debug=true”指令。例如:
- <%@ Page Language="C#" Debug="true" %>
- 將以下的節添加到應用程序的配置文件中:
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
請注意,第二個步驟將使給定應用程序中的所有文件在調試模式下進行編譯;第一個步驟僅使該特定文件在調試模式下進行編譯。
3.添加Page_Error事件,這也是推薦做法。
protected void Page_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); if (ex is HttpRequestValidationException) { Response.Write("請您輸入合法字符串。"); Server.ClearError(); // 如果不ClearError()這個異常會繼續傳到Application_Error()。 } }
4. javascript判斷,個人認為也很好。
<script type="text/jscript">
function validate() {//原諒我新學的javascript。。。
var text = document.getElementById("TextBox1").value;
var testArray = text.split('');
var flag = 0;
for (var a in testArray) {
if (testArray[a] == '<' && flag == 0) {
flag = 1;
}
if (testArray[a] == '>' && flag == 1) {
flag = 2;
break;
}
}
if (flag == 2) {
alert("have <html> tag");
return false;
}
return true;
}
</script>
用javascript我們也可以在需要允許用戶輸入html標簽且不關閉請求驗證的前提下我們可以用javascript進行一個字符的轉義。這樣我們就能夠在后台的page_load階段對隱藏字符串進行操作了。
<script type="text/javascript">
function validate() {
var text = document.getElementById("TextBox1").value;
var testArray = text.split('');
var flag = 0;
for (var a in testArray) {
if (testArray[a] == '<') {
testArray[a] = '<'//convert it to the legitimate string
}
if (testArray[a] == '>') {
testArray[a] = '>'
}
}
document.getElementById("TextBox1").value = testArray.join('');
document.getElementById("HiddenField1").value = testArray.join('');
return true;
}
</script>