RequestValidator 類別

定義

定義自訂要求驗證的基底方法。

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 不會驗證要求。 例如,除非程式碼存取 QueryString 集合,否則 ASP.NET 不會驗證查詢字串值。 根據預設,ASP.NET 也不會驗證某些類型的要求資料,例如表單值、Cookie、使用 HTTP 上傳的 RawUrl 檔案名,以及 屬性的值。

類別 RequestValidator 是您可以實作的基類,以便提供自訂要求驗證。 藉由實作此類別,您可以判斷何時進行驗證,以及要執行驗證的要求資料類型。

根據預設,ASP.NET 提供跨網站腳本 (XSS) 檢查。 不過,您可以藉由建立 XSS 的自訂實作,來補充或取代 ASP.NET 中提供的要求驗證邏輯。 例如,您可以撰寫自訂要求驗證實作,除了檢查 XSS 攻擊之外,還會掃描 SQL 插入式攻擊。

若要建立自訂要求驗證,您可以撰寫衍生自基類的 RequestValidator 自訂類別。 接著,您會將 ASP.NET 設定為在應用層級Web.config檔案中使用自訂要求驗證程式。 您可以將自訂類別放在 App_Code 資料夾中、Bin 資料夾中的已編譯類別庫,或放在 GAC 的已編譯類別庫中。

注意

應用程式只能設定一個自訂要求驗證類型。 無法為個別虛擬路徑或頁面設定不同的要求驗證類型。

建構函式

RequestValidator()

初始化 RequestValidator 類別的新執行個體。

屬性

Current

取得或設定將在應用程式中使用之目前 RequestValidator 執行個體的參考。

方法

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)

適用於

另請參閱