傳統驗證需檢查所有欄位,若表單欄位極多,可採用隨機抽樣:每次回發只驗證隨機挑選的幾個欄位,降低伺服器負擔,同時提高攻擊成本。
public static int RandomIndex(int maxExclusive)
{
return new Random().Next(0, maxExclusive);
}
int i = RandomIndex(8); // 假設欄位為 txt0 ~ txt7
if (!string.IsNullOrEmpty(Request.Form["txt" + i]))
{
// 對該欄位進行詳細驗證
}
foreach (string key in Request.Form)
{
if (!string.IsNullOrEmpty(Request.Form[key]))
{
// 驗證邏輯
}
}
注意:需排除非表單欄位(如 __VIEWSTATE、__EVENTVALIDATION)。