ASP.NET Event Validation
[http://asp.net ASP.net] 2.0 添加了 event validation 的功能, 用來檢查張 form 的 variable, 如果張 form 係一開始時無將 possible variable encode 入個 session 度, 就會 throw exception:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation=”true”/> in configuration or <%@ Page EnableEventValidation=”true” %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
呢個係防止 [http://weblogs.asp.net/bleroy/archive/2004/08/18/216861.aspx injection attack] 既設計, 但是同時有部份 client script 也會出錯, 例如在 asp.net render DropDownList 時,會record 晒所有有可能既 value, 儲係個 page 度
這個過程發生在control的Render()方法中
當頁面postback時,ASP.NET會根據這個隱藏值檢查postback values,如果找不到對應信息,就會throw exception. 所以所有有關 client script 改動 list control 既動作就會有影響
解決方法
1. 禁止這個功能, 但同時會失去一些安全保障:
//—-通過web.config
//—-針對某個page
<%@ Page EnableEventValidation="false" … %>
2.Register For Event Validation
其原理就是讓asp.net記錄這個postback value.
RegisterForEventValidation必須在render() 內使用.
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation(_recipeList.UniqueID,"4");
// 4 係其中一個 possible value
base.Render(writer);
}
如果我們自己寫了一個control,需要使用validate events功能,就需要使用SupportsEventValidation attribute,
[SupportsEventValidation]
public class DynamicDropDownList : DropDownList
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(this.UniqueID, "4");
// 4 係其中一個 possible value, 如果多過一個就要每個 register 一次
base.Render(writer);
}
}
目前,asp.net還不能單獨禁止某個control的validate events功能.
Tags: asp.net

