Export (0) Print
Expand All

RequestValidator Class

Defines base methods for custom request validation.

System.Object
  System.Web.Util.RequestValidator

Namespace:  System.Web.Util
Assembly:  System.Web (in System.Web.dll)

public class RequestValidator

The RequestValidator type exposes the following members.

  NameDescription
Public methodRequestValidatorInitializes a new instance of the RequestValidator class.
Top

  NameDescription
Public propertyStatic memberCurrentGets or sets a reference to the current RequestValidator instance that will be used in an application.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInvokeIsValidRequestStringProvides a public method that calls the protected IsValidRequestString method in order to validate HTTP request data.
Protected methodIsValidRequestStringValidates a string that contains HTTP request data.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

By default, ASP.NET does not validate requests until code explicitly requests a value from the request. For example, ASP.NET does not validate query-string values until code accesses the QueryString collection. By default, ASP.NET also does not validate some types of request data, such as form values, cookies, the names of files that have been uploaded using HTTP, and the value of the RawUrl property.

The RequestValidator class is a base class that you can implement in order to provide custom request validation. By implementing this class, you can determine when validation occurs and what type of request data to perform validation on.

By default, ASP.NET provides cross-site scripting (XSS) checks. However, you can supplement or replace the request validation logic that is provided in ASP.NET by creating a custom implementation of the XSS. For example, you can write a custom request validation implementation that scans for SQL injection attacks in addition to checking for XSS attacks.

To create custom request validation, you write a custom class that derives from the RequestValidator base class. You then configure ASP.NET to use the custom request validator in the application-level Web.config file. You can put the custom class in the App_Code folder, in a compiled class library in the Bin folder, or in a compiled class library in the GAC.

NoteNote

Only one custom request validation type can be configured for an application. It is not possible to configure a different request validation type for individual virtual paths or pages.

The following example shows how to create a custom request validator that allows only a specific string to be used for query-string values.

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

The following example shows how to configure ASP.NET to use the custom validator in the Web.config file for an application.

<system.web>
  <httpRuntime requestValidationType="CustomRequestValidation" />
</system.web>

.NET Framework

Supported in: 4.6, 4.5, 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft