RequestValidationSource 列挙型

定義

検証する HTTP 要求データの種類を指定します。

public enum class RequestValidationSource
public enum RequestValidationSource
type RequestValidationSource = 
Public Enum RequestValidationSource
継承
RequestValidationSource

フィールド

名前 説明
QueryString 0

クエリ文字列。

Form 1

フォーム値。

Cookies 2

要求 Cookie。

Files 3

アップロードされたファイル。

RawUrl 4

生の URL。 (ドメインの後の URL の部分)。

Path 5

仮想パス。

PathInfo 6

HTTP PathInfo 文字列。これは URL パスの拡張機能です。

Headers 7

要求ヘッダー。

次の例は、クエリ文字列値に対して特定の文字列のみを許可するカスタム要求検証コントロール クラスを作成する方法を示しています。

Imports System.Web
Imports System.Web.Util

Public Class CustomRequestValidation
    Inherits RequestValidator

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 for RequestValidationSource.RawUrl.
    If requestValidationSource = RequestValidationSource.RawUrl Then
        Return True
    End If

    ' Allow the query-string key "data" to have an XML-like value.
    If (requestValidationSource = _
            (RequestValidationSource.QueryString) AndAlso _
            (collectionKey = "data") Then
        ' The querystring 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 fall back to
        ' the base ASP.NET implementation.
        Return MyBase.IsValidRequestString(context, value, _
            requestValidationSource__1, collectionKey, _
            validationFailureIndex)
    End If
End Function
End Class
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)
    {
        //Set a default value for the out parameter.
        validationFailureIndex = -1;

        // This application does not use RawUrl directly,
        // so you can ignore the check for RequestValidationSource.RawUrl.
        if (requestValidationSource == RequestValidationSource.RawUrl)
            return true;

        // Allow the query-string key "data" to have an XML-like value.
        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 fall back to
        // the base ASP.NET implementation.
        else
        {
            return base.IsValidRequestString(context, value,
                requestValidationSource, collectionKey,
                out validationFailureIndex);
        }
    }
}

次の例は、カスタム検証コントロールを使用するように ASP.NET を構成する方法を示しています。

<httpRuntime requestValidationType="CustomRequestValidation" />

注釈

RequestValidator型を実装することで、カスタム要求の検証の種類を作成できます。 ASP.NETIsValidRequestString メソッドを呼び出して要求を検証 ASP.NET、requestValidationSource パラメーターを渡して、検証するデータのソースを指定します。 RequestValidationSource列挙型は、検証対象の要求データのソースまたは種類を指定するために使用されます。 列挙型は、value メソッドのIsValidRequestString パラメーターで渡される HTTP 入力の種類を示します。 カスタム ロジックを使用して検証しない場合は、HTTP 入力の基本要求検証実装にフォールバックする方法として列挙を使用できます。

次の表は、collectionKey メソッドの value パラメーターと RequestValidator.IsValidRequestString パラメーターの値が、RequestValidationSource 列挙体の各メンバーに対してどのように解釈されるかを示しています。

列挙メンバー collectionKey パラメーター value パラメーター
Cookies コレクション内の Cookie の名前。 コレクション内の値。
Files コレクション内のアップロードされたファイルの名前。 コレクション内のアップロードされたファイルの値。
Form コレクション内のフォーム パラメーターの名前 コレクション内のフォーム パラメーターの値。
Headers コレクション内の HTTP ヘッダーの名前。 コレクション内の HTTP ヘッダーの値。
Path null (Path は値のコレクションではありません)。 Path フィールドの値。
PathInfo null (PathInfo は値のコレクションではありません)。 PathInfo フィールドの値。
QueryString コレクション内のクエリ文字列パラメーターの名前。 コレクション内のクエリ文字列パラメーターの値。
RawUrl null (RawUrl は値のコレクションではありません)。 RawUrl フィールドの値。

適用対象

こちらもご覧ください