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 不会验证请求。 例如,在代码访问 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) |