System.Web.HttpRequestValidationException异常的最好解决方法
2014-04-24 00:15
806 查看
从客户端(TextBox1="<td>")中检测到有潜在危险的 Request.Form 值。 说明: 请求验证过程检测到有潜在危险的客户端输入值,对请求的处理已经中止。该值可能指示存在危及应用程序安全的尝试,如跨站点脚本攻击。若要允许页面重写应用程序请求验证设置,请将 httpRuntime 配置节中的 requestValidationMode 特性设置为 requestValidationMode="2.0"。示例: <httpRuntime requestValidationMode="2.0" />。设置此值后,可通过在 Page 指令或 <pages> 配置节中设置 validateRequest="false" 禁用请求验证。但是,在这种情况下,强烈建议应用程序显式检查所有输入。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkId=153133。 异常详细信息: System.Web.HttpRequestValidationException: 从客户端(TextBox1="<td>")中检测到有潜在危险的 Request.Form 值。 其实description 已经讲得很明白了,当我们试图在文本框内输入html标签的时候(这里输入的<td>),request validation的时候为了防止危险的客户端输入就会抛HttpRequestValidationException。 至于为什么要进行这样的验证呢? 下面这篇文章介绍的很好。 警告:为了安全请不要随意将ASP.Net的validateRequest="false" 我们在这里的解决途径有以下几种: 1. disable validate request(不管推不推荐这是最普遍的) 在3.5及以下版本中 我们可以用如下几种操作: ASP.Net页面描述中通过设置 validateRequest=false 来禁用这个特性 在web.config中配置 <configuration> <system.web> <pages validateRequest="false" /> </system.web> </configuration> 在4.0中我们则不能够这样做了,但是我们可以通过重写RequestValidator中的IsValidRequestString 来达到同样的效果。 首先新建一个RequestValidatorDisabled类。 using System;class RequestValidatorDisabled : System.Web.Util.RequestValidator { protected override bool IsValidRequestString(System.Web.HttpContext context, string value, System.Web.Util.RequestValidationSource requestValidationSource, string collectionKey, out int validationFailureIndex) { validationFailureIndex = -1; return true; }} 然后在webconfig system.web下加上 <httpRuntime requestValidationType="RequestValidatorDisabled" /> 这样也可以有相同的效果 2.在产生错误的文件的顶部添加一条“Debug=true”指令。例如: <%@ Page Language="C#" Debug="true" %> 将以下的节添加到应用程序的配置文件中: <configuration> <system.web> <compilation debug="true"/> </system.web> </configuration> 请注意,第二个步骤将使给定应用程序中的所有文件在调试模式下进行编译;第一个步骤仅使该特定文件在调试模式下进行编译。 3.添加Page_Error事件,这也是推荐做法。 protected void Page_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); if (ex is HttpRequestValidationException) { Response.Write("请您输入合法字符串。"); Server.ClearError(); // 如果不ClearError()这个异常会继续传到Application_Error()。 } } 4. javascript判断,个人认为也很好。 <script type="text/jscript"> function validate() {//原谅我新学的javascript。。。 var text = document.getElementById("TextBox1").value; var testArray = text.split(''); var flag = 0; for (var a in testArray) { if (testArray[a] == '<' && flag == 0) { flag = 1; } if (testArray[a] == '>' && flag == 1) { flag = 2; break; } } if (flag == 2) { alert("have <html> tag"); return false; } return true; } </script> 用javascript我们也可以在需要允许用户输入html标签且不关闭请求验证的前提下我们可以用javascript进行一个字符的转义。这样我们就能够在后台的page_load阶段对隐藏字符串进行操作了。 <script type="text/javascript"> function validate() { var text = document.getElementById("TextBox1").value; var testArray = text.split(''); var flag = 0; for (var a in testArray) { if (testArray[a] == '<') { testArray[a] = '<'//convert it to the legitimate string } if (testArray[a] == '>') { testArray[a] = '>' } } document.getElementById("TextBox1").value = testArray.join(''); document.getElementById("HiddenField1").value = testArray.join(''); return true; } </script>
相关文章推荐
- System.Web.HttpRequestValidationException:解决方法
- System.Web.HttpRequestValidationException:解决方法
- WebServcies 调用方法异常:System.Web.HttpRequestValidationException: 从客户端中检测到有潜在危险的 Request.Form 值。
- System.Web.HttpRequestValidationException解决方法
- System.Web.HttpRequestValidationException解决方法
- 异常详细信息: System.Web.HttpRequestValidationException: 从客户端(fck_content="
- 异常详细信息: System.Web.HttpRequestValidationException: 从客户端(fck_content="<span style="bac...")中检测到有潜在危
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- System.Web.HttpRequestValidationException——从客户端检测到危险的Request值
- System.Web.HttpRequestValidationException: 从客户端(XML="<?xml version=\"1.0\...")中检测到
- System.Web.HttpRequestValidationException: 从客户端(dbFlag="<soap:Envelope xmlns...")中检测到有潜在危险的 Request.Form 值。
- System.Web.HttpRequestValidationException——从客户端检测到危险的Request值
- 异常详细信息: System.Web.Hosting.HostingEnvironmentException: 访问 IIS 元数据库失败 解决方法
- System.Web.HttpRequestValidationException: 从客户端(name="<a href=''>我是晓菜鸟</a>")中检测到有潜在危险的 Request.Form 值
- System.Web.HttpRequestValidationException: 从客户端(txtName="<fd>")中检测到有潜在危险的 Request.Form 值
- 如何避免 System.Web.HttpRequestValidationException
- 解决在运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度问
- 异常详细信息: System.Web.Hosting.HostingEnvironmentException: 访问 IIS 元数据库失败 解决方法
- HttpRequestValidationException (0x80004005) 的三种可能的解决方法
- System.Web.HttpRequestValidationException: 从客户端(txtName="<fd>")中检测到有潜在危险的 Request.Form 值。