ASP.NET: The Custom RequestValidator
来源:互联网 发布:mac通过蓝牙传输文件 编辑:程序博客网 时间:2024/06/05 04:13
首先我们先来看看 PagesSection.ValidateRequest 属性
<%@ Page validateRequest="true" %>
请注意:此属性在 .NET Framework 2.0 版中是新增的。
该属性是获取或设置一个值,该值确定 ASP.NET 是否针对危险值来检查来自浏览器的输入。默认为true。
如果为true,就会通过将所有输入数据与潜在危险值的列表进行比较来执行请求验证。如果出现匹配,则 ASP.NET 引发 HttpRequestValidationException。
=======================================================================
在 Web 应用程序中,要阻止依赖于恶意输入字符串的黑客攻击,约束和验证用户输入是必不可少的。跨站点脚本攻击就是此类攻击的一个示例。通过在Page节点validateRequest = "true"的设置,能够显式检查来自浏览器的输入。
我们可以根据自身网站特点来进行设置validateRequest的值,如网站页面允许编辑html内容,可以设置validateRequest = false,不启用请求验证;假若我们只是想允许某个或某几个标签(危险值)通过页面输入保存时,建议不要设置validateRequest = false,更好的解决方案是使用自定义的请求验证类。
web.config 的代码:
再来看看HttpRuntimeSection.RequestValidationMode 属性
该属性是获取或设置一个版本号,该版本号指示将使用哪种 ASP.NET 版本特定的验证方法。
该属性可以设置为下列值:
由于 requestValidationMode="4.0" 是强制启用验证,所以我们会发现在 .NET Framework 4.0 中仅靠设置 validateRequest 是关闭不了请求验证的,还需将 requestValidationMode 设置为 2.0。
ASP.NET中的请求验证特性提供了某一等级的保护措施防止XSS攻击,4.0 之前版本的ASP.NET的请求验证是默认启动的,但是他紧紧应用于ASP.NET页面中(.aspx文件和.aspx.cs文件)。
而在ASP.NET4中,请求验证默认对所有类型的请求启动,因为它在BeginRequest被调用之前启动,结果就是对所有资源的请求都要经过请求验证,而不仅仅在.aspx文件和他们的类文件中,甚至包括web service和自定义的httphandler。同样,在自定义httpmodules读取http请求的时候,同样要经过请求验证。
因此若关闭请求验证,需要将验证模式设置为ASP.NET之前的版本。(requestValidationMode = "2.0")
最后来看看自定义的请求验证, RequestValidationType 属性
首先需要在web.config中指定自己实现的请求验证类类型,这个类必须从RequestValidator类中继承。
上述CustomValidator是基于基类的验证上,允许特殊标签"&#"通过验证。
<%@ Page validateRequest="true" %>
请注意:此属性在 .NET Framework 2.0 版中是新增的。
该属性是获取或设置一个值,该值确定 ASP.NET 是否针对危险值来检查来自浏览器的输入。默认为true。
如果为true,就会通过将所有输入数据与潜在危险值的列表进行比较来执行请求验证。如果出现匹配,则 ASP.NET 引发 HttpRequestValidationException。
=======================================================================
在 Web 应用程序中,要阻止依赖于恶意输入字符串的黑客攻击,约束和验证用户输入是必不可少的。跨站点脚本攻击就是此类攻击的一个示例。通过在Page节点validateRequest = "true"的设置,能够显式检查来自浏览器的输入。
我们可以根据自身网站特点来进行设置validateRequest的值,如网站页面允许编辑html内容,可以设置validateRequest = false,不启用请求验证;假若我们只是想允许某个或某几个标签(危险值)通过页面输入保存时,建议不要设置validateRequest = false,更好的解决方案是使用自定义的请求验证类。
web.config 的代码:
<system.web>
<httpRuntime requestValidationMode= "2.0"
requestValidationType = "MyNameSpace.Web.BaseCode.CustomValidator"/>
</system.web>
<httpRuntime requestValidationMode= "2.0"
requestValidationType = "MyNameSpace.Web.BaseCode.CustomValidator"/>
</system.web>
再来看看HttpRuntimeSection.RequestValidationMode 属性
该属性是获取或设置一个版本号,该版本号指示将使用哪种 ASP.NET 版本特定的验证方法。
该属性可以设置为下列值:
- 4.0。 HttpRequest 对象在内部设置一个标志,指示访问任何 HTTP 请求数据时,应触发请求验证。 这保证了请求验证触发之前的数据,例如 cookie,并在请求过程中访问的 Url。此时强制启用验证,不管@Page validateRequest 为何值。(.NET Framework 4下为默认值)
- 2.0。 仅对于页面,不适用于所有 HTTP 请求启用请求验证。是启用还是关闭取决于@Page validateRequest。
由于 requestValidationMode="4.0" 是强制启用验证,所以我们会发现在 .NET Framework 4.0 中仅靠设置 validateRequest 是关闭不了请求验证的,还需将 requestValidationMode 设置为 2.0。
ASP.NET中的请求验证特性提供了某一等级的保护措施防止XSS攻击,4.0 之前版本的ASP.NET的请求验证是默认启动的,但是他紧紧应用于ASP.NET页面中(.aspx文件和.aspx.cs文件)。
而在ASP.NET4中,请求验证默认对所有类型的请求启动,因为它在BeginRequest被调用之前启动,结果就是对所有资源的请求都要经过请求验证,而不仅仅在.aspx文件和他们的类文件中,甚至包括web service和自定义的httphandler。同样,在自定义httpmodules读取http请求的时候,同样要经过请求验证。
因此若关闭请求验证,需要将验证模式设置为ASP.NET之前的版本。(requestValidationMode = "2.0")
最后来看看自定义的请求验证, RequestValidationType 属性
首先需要在web.config中指定自己实现的请求验证类类型,这个类必须从RequestValidator类中继承。
namespace MyNameSpace.Web.BaseCode{ /// <summary> /// The Custom Validator /// </summary> public class CustomValidator : RequestValidator { /// <summary> /// Validate request data whether is valid. /// </summary> /// <param name="context">The context of the current request.</param> /// <param name="value">The HTTP request data to validate.</param> /// <param name="requestValidationSource"> /// An enumeration that represents the source of request data that is being validated. /// The following are possible values for the enumeration: /// QueryString, Form, Cookies, Files, RawUrl, Path, PathInfo, Headers. /// </param> /// <param name="collectionKey"> /// The key in the request collection of the item to validate. /// This parameter is optional. /// This parameter is used if the data to validate is obtained from a collection. /// If the data to validate is not from a collection, collectionKey can be null. /// </param> /// <param name="validationFailureIndex"> /// When this method returns, indicates the zero-based starting point of the problematic or invalid text in the request collection. /// This parameter is passed uninitialized. /// </param> /// <returns>true if the string to be validated is valid; otherwise, false.</returns> protected override bool IsValidRequestString(HttpContext context, string value, RequestValidationSource requestValidationSource, string collectionKey, out int validationFailureIndex) { string ignoreString = "&#"; if (requestValidationSource == RequestValidationSource.Form && value.Contains(ignoreString)) { // delete the ignoreString from value. while (true) { int index = value.IndexOf(ignoreString); if (index < 0) { break; } else { value = value.Substring(0, index) + value.Substring(index + ignoreString.Length); } } } return base.IsValidRequestString(context, value, requestValidationSource, collectionKey, out validationFailureIndex); } }}
上述CustomValidator是基于基类的验证上,允许特殊标签"&#"通过验证。
0 0
- ASP.NET: The Custom RequestValidator
- Implement Custom Paging in the ASP.Net Datagrid Control...
- ASP.NET MVC3 Custom FormAuthorize
- On the Way to Mastering ASP.NET: Introducing Custom Entity Classes
- Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
- Asp.Net Mvc: Implement your custom ModelBinder
- Custom DialogBox Control Component For ASP.net
- ASP.NET MVC3 Custom ErrorPages 500/404
- Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0(登录控件的验证原理)
- Resolving the Unknown: Building Custom XmlResolvers in the .NET Framework
- 关于ASP.NET MVC和NHibernate,LinQ,ASP.NET AJAX,ASP.NET Custom Server Control
- Developing ASP.NET Custom Control With C# Builder
- ASP.NET: Custom AutoCompleteTextBox WebControl [With Source Code]
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder
- Balanced Binary Tree
- poj3628Bookshelf 2
- 关于eclipse的部分知识
- return str引出的内存分配方式---栈和堆的区别
- iOS8下,UIImagePickerController的popoverPresentationController为空,要怎么获取?
- ASP.NET: The Custom RequestValidator
- CSRFGuard 3 Token Injection
- 关于修饰词const
- 规范化轴的最大最小值 和 间隔
- 笔试题输入一串字符,倒叙单词输出,结尾符号不变
- CentOS 7 64bit系统上编译安装PHP 5.6.13
- Django Template 路径配置
- Apache CXF实战之二 传输pojo对象
- Android中SQLite使用