RequestValidator 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義了自訂請求驗證的基礎方法。
public ref class RequestValidator
public class RequestValidator
type RequestValidator = class
Public Class RequestValidator
- 繼承
-
RequestValidator
範例
以下範例展示如何建立一個自訂請求驗證器,該驗證器只允許使用特定字串作為查詢字串值。
using System;
using System.Web;
using System.Web.Util;
public class CustomRequestValidation : RequestValidator
{
public CustomRequestValidation() { }
protected override bool IsValidRequestString(
HttpContext context, string value,
RequestValidationSource requestValidationSource, string collectionKey,
out int validationFailureIndex)
{
validationFailureIndex = -1; //Set a default value for the out parameter.
//This application does not use RawUrl directly so you can ignore the check.
if (requestValidationSource == RequestValidationSource.RawUrl)
return true;
//Allow the query-string key data to have a value that is formatted like XML.
if ((requestValidationSource == RequestValidationSource.QueryString) &&
(collectionKey == "data"))
{
//The querystring value "<example>1234</example>" is allowed.
if (value == "<example>1234</example>")
{
validationFailureIndex = -1;
return true;
}
else
//Leave any further checks to ASP.NET.
return base.IsValidRequestString(context, value,
requestValidationSource,
collectionKey, out validationFailureIndex);
}
//All other HTTP input checks are left to the base ASP.NET implementation.
else
{
return base.IsValidRequestString(context, value, requestValidationSource,
collectionKey, out validationFailureIndex);
}
}
}
Imports System.Web
Imports System.Web.Util
Public Class CustomRequestValidation
Inherits RequestValidator
Public Sub New()
End Sub
Protected Overloads Overrides Function IsValidRequestString(ByVal context As HttpContext, ByVal value As String, _
ByVal requestValidationSource__1 As RequestValidationSource, _
ByVal collectionKey As String, _
ByRef validationFailureIndex As Integer) As Boolean
validationFailureIndex = -1 ' Set a default value for the out parameter.
' This application does not use RawUrl directly so you can ignore the check.
If requestValidationSource__1 = RequestValidationSource.RawUrl Then
Return True
End If
' Allow the query-string key data to have a value that is formated like XML.
If (requestValidationSource__1 = RequestValidationSource.QueryString) AndAlso (collectionKey = "data") Then
' The query-string value "<example>1234</example>" is allowed.
If value = "<example>1234</example>" Then
validationFailureIndex = -1
Return True
Else
' Leave any further checks to ASP.NET.
Return MyBase.IsValidRequestString(context, value,
requestValidationSource__1, collectionKey,
validationFailureIndex)
End If
Else
' All other HTTP input checks are left to the base ASP.NET implementation.
Return MyBase.IsValidRequestString(context, value, requestValidationSource__1, collectionKey, validationFailureIndex)
End If
End Function
End Class
以下範例說明如何配置 ASP.NET 在應用程式的 Web.config 檔案中使用自訂驗證器。
<system.web>
<httpRuntime requestValidationType="CustomRequestValidation" />
</system.web>
備註
預設情況下,ASP.NET 不會驗證請求,除非程式碼明確向請求請求請求中請求一個值。 例如,ASP.NET 在程式碼存取 QueryString 集合前不會驗證查詢字串值。 預設情況下,ASP.NET 也不會驗證某些請求資料,例如表單值、Cookie、已上傳 HTTP 檔案名稱,以及屬性 RawUrl 的值。
這個 RequestValidator 類別是一個基底類別,你可以實作它來提供自訂的請求驗證。 透過實作此類別,您可以判斷驗證何時發生,以及對哪種請求資料進行驗證。
預設情況下,ASP.NET 提供跨站腳本(XSS)檢查。 不過,你也可以透過建立自訂的 XSS 實作來補充或取代 ASP.NET 中提供的請求驗證邏輯。 例如,你可以寫一個自訂的請求驗證實作,除了檢查 XSS 攻擊外,還會掃描 SQL 注入攻擊。
要建立自訂請求驗證,你要寫一個從 RequestValidator 基底類別衍生的自訂類別。 接著你在應用程式層級的 Web.config 檔案中設定 ASP.NET 使用自訂請求驗證器。 你可以把自訂類別放在 App_Code 資料夾、Bin 資料夾的已編譯類別庫,或是 GAC 裡的編譯類別庫。
備註
應用程式只能設定一種自訂請求驗證類型。 無法為個別虛擬路徑或頁面設定不同的請求驗證類型。
建構函式
| 名稱 | Description |
|---|---|
| RequestValidator() |
初始化 RequestValidator 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| Current |
取得或設定一個將用於應用程式的當前 RequestValidator 實例參考。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| InvokeIsValidRequestString(HttpContext, String, RequestValidationSource, String, Int32) |
提供一個公開方法,呼叫受保護 IsValidRequestString(HttpContext, String, RequestValidationSource, String, Int32) 的方法以驗證 HTTP 請求資料。 |
| IsValidRequestString(HttpContext, String, RequestValidationSource, String, Int32) |
驗證包含 HTTP 請求資料的字串。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |