在.NET 4.0中,請求驗證被加快了一個檔次!看起來,當任何代碼嘗試訪問Request.Form時,表單中的值將被驗證,並引發HttpRequestValidationException。HttpRequestValidationException重定向到登錄頁面(表單身份驗證)
這本身不應該是一個問題,除了我發現一個特定的場景,ASP.NET本身在處理這種情況時肯定不會對我有任何幫助。
我有一個接受HTML輸入的表單(我寧願不要,但是我確定,好嗎?)。該表單的編輯模型在字段上具有[AllowHtml]
屬性,因此提交HTML很好,並且不會拋出異常。
但是,如果用戶沒有通過驗證(例如,他們的會話超時),他們試圖提交表單,一個HttpRequestValidationException被拋出,而ASP.NET正試圖重定向到登錄頁面。 ASP.NET驗證請求是否是AJAX請求時會引發異常,此時它會嘗試訪問表單字段。
堆棧跟蹤:內System.Web.UI.PageRequestManager.IsAsyncPostBackRequest
的代碼
[HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (LongDescription="<p>Hello</p>").]
System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection) +8855748
System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, RequestValidationSource requestCollection) +122
System.Web.HttpRequest.get_Form() +150
System.Web.HttpRequestWrapper.get_Form() +11
System.Web.UI.PageRequestManager.IsAsyncPostBackRequest(HttpRequestBase request) +223
System.Web.Handlers.ScriptModule.HttpResponse_Redirecting(Object sender, EventArgs e) +82
System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent) +394
System.Web.Security.FormsAuthenticationModule.OnLeave(Object source, EventArgs eventArgs) +9044409
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
不對行:
// DevDiv Bugs 188713: X-MicrosoftAjax header is stripped by some firewalls
string asyncPost = request.Form[AsyncPostFormField];
return !String.IsNullOrEmpty(asyncPost) && (asyncPost.Trim() == "true");
所有我想是因爲它成功地重定向到登錄頁面,在這種情況下。我也想不要禁用.NET 4.0驗證!
任何人有任何想法如何克服這個問題?
感謝您的回答,但應用URL編碼爲HTML不會是正確的 - 這將是HTML編碼(例如,在與URL編碼爲%26,但在HTML是&此外,我需要這個。適用於允許使用HTML的所有表單 - 僅適用於重定向到登錄頁面的一種情況... – 2012-01-10 15:59:06