从客户端中检测到有潜在危险的 Request.Form 值"的解决方案汇总
阅读原文时间:2021年04月20日阅读:1

目录


说明

        在一个asp.net 的项目中,前端通过ajax将富文本中的文字内容post到服务端的一个ashx中,在ashx中尝试读取参数值时,结果报错:“从客户端中检测到有潜在危险的 Request.Form 值”
        由于在asp.net中,Request提交时出现有html代码字符串时,程序系统会认为其具有潜在危险的值。会报出“从客户端 中检测到有潜在危险的Request.Form值”这样的Error。

        而富文本中的内容是包含html代码的,所以…


解决方案

1、前端对富文本字符串进行encodeURI编码,服务端进行HttpUtility.UrlDecode解码操作;

前端代码:

    var str = '<p><span style="color: #00B0F0;"><em><strong>我想留在你的身边,</strong></em></span><br/></p><p><span style="color: #7030A0;"><strong><span style="text-decoration: underline;">深情款款多么可怜;</span></strong></span></p>';
    $(function() {
        $.ajax({
            type: "post",
            url: "TestHandle.ashx",
            data: { Title: 'jack', Content: encodeURI(str) },
            success: function (data) {
                $("#div").html(data);
            }
        });
    });

后端代码:

    public void ProcessRequest(HttpContext context)
    {
        string str = context.Request["content"];
        string content = HttpUtility.UrlDecode(str);
        context.Response.ContentType = "text/plain";
        context.Response.Write(content);
    }

2、前端不以form的方式提交,直接以json方式提交,服务端从request的body中读取数据,然后反序列化,得到信息;

前端代码:

    var str = '<p><span style="color: #00B0F0;"><em><strong>我想留在你的身边,</strong></em></span><br/></p><p><span style="color: #7030A0;"><strong><span style="text-decoration: underline;">深情款款多么可怜;</span></strong></span></p>';
    var temp = { Title: 'jack', Content: str };
    $.ajax({
        type: "post",
        url: "TestHandle.ashx",
        contentType:"application/json;charset=utf-8",
        data: JSON.stringify(temp),
        success: function (data) {
            $("#div").html(data);
        }
    });

后端代码:

    string bodyText;
    using (var bodyReader = new System.IO.StreamReader(context.Request.InputStream))
    {
        bodyText = bodyReader.ReadToEnd();
    }
    dynamic bodyObj = JsonConvert.DeserializeObject(bodyText);

    context.Response.ContentType = "text/plain";
    context.Response.Write(bodyObj.Content);

3、aspx页面,当前页面进行form提交

打开当前.aspx页面,页头加上代码:validateRequest=”false”,如:

<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="false" CodeFile="default.aspx.cs" Inherits="default" %>

该方法不推荐,还有一种修改web.config配置文件的方法,强烈不推荐

<configuration>
  <system.web>
    <httpRuntime requestValidationMode="2.0" />
    <pages validateRequest="false" />  
  </system.web>
</configuration>

4、在ASP.NET MVC中的解决方案

针对某个实体类的单个字段设置 [AllowHtml] ,这样提交的时候,系统就会放过该字段。

前端代码:

    var str = '<p><span style="color: #00B0F0;"><em><strong>我想留在你的身边,</strong></em></span><br/></p><p><span style="color: #7030A0;"><strong><span style="text-decoration: underline;">深情款款多么可怜;</span></strong></span></p>';
    $(function () {
        $.ajax({
            type: "post",
            url: "Home/Test",
            data: { Title: 'jack', Content: str },
            success: function (data) {
                $("#div").html(data.ok);
            }
        });
    });

后端代码:

    public class NewInfo
    {
         public string Title { get; set; }
         [AllowHtml]
         public string Content { get; set; }
    }
    public ActionResult Test(NewInfo info)
    {
        return Json(new { ok = info.Content});
    }