System.Web.HttpRequestValidationException解决方法

从客户端(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>),requestvalidation的时候为了防止危险的客户端输入就会抛HttpRequestValidationException

至于为什么要进行这样的验证呢?

下面这篇文章介绍的很好。

警告:为了安全请不要随意将ASP.NetvalidateRequest="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类。

 

[csharp] view plaincopy

1.  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下加上

<httpRuntimerequestValidationType="RequestValidatorDisabled" />

这样也可以有相同的效果

 

2.在产生错误的文件的顶部添加一条“Debug=true”指令。例如

  • <%@ Page Language="C#" Debug="true" %>
  • 将以下的节添加到应用程序的配置文件中:

<configuration>

<system.web>

<compilation debug="true"/>
</system.web>
</configuration>
请注意,第二个步骤将使给定应用程序中的所有文件在调试模式下进行编译;第一个步骤仅使该特定文件在调试模式下进行编译。

 

3.添加Page_Error事件,这也是推荐做法。

[csharp] view plaincopy

1.  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">

 

        functionvalidate() {

 

           var text = document.getElementById("TextBox1").value;

 

           var testArray = text.split('');

 

           

 

           var flag = 0;

 

           for (var a in testArray) {

 

               

 

               if (testArray[a] == '<') {

 

                   testArray[a] = '&lt'//convert it to the legitimate string 

 

               }

 

 

 

               if (testArray[a] == '>') {

 

                   testArray[a] = '&gt'

 

               }

 

           }

 

           document.getElementById("TextBox1").value =http://www.cnblogs.com/he-yuan/archive/2012/01/16/testArray.join('');

 

           document.getElementById("HiddenField1").value =http://www.cnblogs.com/he-yuan/archive/2012/01/16/testArray.join('');

 

           return true;

 

        }

 

  </script>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值