RequestValidationSource Enum

Definition

Anger vilken typ av HTTP-begärandedata som ska verifieras.

public enum class RequestValidationSource
public enum RequestValidationSource
type RequestValidationSource = 
Public Enum RequestValidationSource
Arv
RequestValidationSource

Fält

Name Värde Description
QueryString 0

Frågesträngen.

Form 1

Formulärvärdena.

Cookies 2

Begärandecookies.

Files 3

Den uppladdade filen.

RawUrl 4

Den råa URL:en. (Delen av en URL efter domänen.)

Path 5

Den virtuella sökvägen.

PathInfo 6

En HTTP-sträng PathInfo , som är ett tillägg till en URL-sökväg.

Headers 7

Begärandehuvuden.

Exempel

I följande exempel visas hur du skapar en anpassad validatorklass för begäran som endast tillåter en specifik sträng för frågesträngsvärden.

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);
        }
    }
}

I följande exempel visas hur du konfigurerar ASP.NET att använda den anpassade validatorn.

<httpRuntime requestValidationType="CustomRequestValidation" />

Kommentarer

Du kan skapa en verifieringstyp för anpassad begäran genom att implementera RequestValidator typen. När ASP.NET anropar metoden IsValidRequestString för att verifiera en begäran skickar ASP.NET en parameter requestValidationSource för att ange källan för de data som verifieras. Uppräkningen RequestValidationSource används för att ange källan eller typen av begärandedata som verifieras. Uppräkningen anger vilken typ av HTTP-indata som skickas i value metodens IsValidRequestString parameter. Du kan använda uppräkningen som ett sätt att återgå till valideringsimplementeringen för basbegäran för HTTP-indata om du inte vill verifiera med hjälp av anpassad logik.

I följande tabell visas hur värdet collectionKey för metoden och value -parametern RequestValidator.IsValidRequestString tolkas för varje medlem i RequestValidationSource uppräkningen.

Uppräkningsmedlem collectionKey Parametern value Parametern
Cookies Namnet på cookien i samlingen. Värdet i samlingen.
Files Namnet på den uppladdade filen i samlingen. Värdet för den uppladdade filen i samlingen.
Form Namnet på formulärparametern i samlingen Värdet för formulärparametern i samlingen.
Headers Namnet på en HTTP-rubrik i samlingen. Värdet för HTTP-huvudet i samlingen.
Path null (Path är inte en samling värden). Värdet för fältet Sökväg.
PathInfo null (PathInfo är inte en samling värden). Värdet för fältet PathInfo.
QueryString Namnet på frågesträngsparametern i samlingen. Värdet för frågesträngsparametern i samlingen.
RawUrl null (RawUrl är inte en samling värden.) Värdet för fältet RawUrl.

Gäller för

Se även