zoukankan      html  css  js  c++  java
  • 【转载】"从客户端中检测到有潜在危险的 Request.Form 值"的解决方案汇总

    "从客户端中检测到有潜在危险的 Request.Form 值"的解决方案汇总

     

    正文

    #事故现场

      在一个asp.net 的项目中,前端通过ajax将富文本中的文字内容post到服务端的一个ashx中,在ashx中尝试读取参数值时,

    结果报错:“从客户端中检测到有潜在危险的 Request.Form 值”

    #事故分析

      由于在asp.net中,Request提交时出现有html代码字符串时,程序系统会认为其具有潜在危险的值。会报出“从客户端 中检测到有潜在危险的Request.Form值”这样的Error。

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

    #解决方案:

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

      前端代码:

    复制代码
     1     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>';
     2     $(function() {
     3         $.ajax({
     4             type: "post",
     5             url: "TestHandle.ashx",
     6             data: { Title: 'jack', Content: encodeURI(str) },
     7             success: function (data) {
     8                 $("#div").html(data);
     9             }
    10         });
    11     });
    复制代码

      后端代码:

    复制代码
    1     public void ProcessRequest(HttpContext context)
    2     {
    3         string str = context.Request["content"];
    4         string content = HttpUtility.UrlDecode(str);
    5         context.Response.ContentType = "text/plain";
    6         context.Response.Write(content);
    7     }
    复制代码

      效果图:

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

      前端代码:

    复制代码
     1     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>';
     2     var temp = { Title: 'jack', Content: str };
     3     $.ajax({
     4         type: "post",
     5         url: "TestHandle.ashx",
     6         contentType:"application/json;charset=utf-8",
     7         data: JSON.stringify(temp),
     8         success: function (data) {
     9             $("#div").html(data);
    10         }
    11     });
    复制代码

      后端代码:

    复制代码
    1     string bodyText;
    2     using (var bodyReader = new System.IO.StreamReader(context.Request.InputStream))
    3     {
    4         bodyText = bodyReader.ReadToEnd();
    5     }
    6     dynamic bodyObj = JsonConvert.DeserializeObject(bodyText);
    7 
    8     context.Response.ContentType = "text/plain";
    9     context.Response.Write(bodyObj.Content);
    复制代码

      效果图:

    #其他场景的解决方案:

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

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

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

      该方法不推荐,还有一种修改web.config配置文件的方法,强烈不推荐,就不写在这里了;

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

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

      2)、前端代码:

    复制代码
     1     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>';
     2     $(function () {
     3         $.ajax({
     4             type: "post",
     5             url: "Home/Test",
     6             data: { Title: 'jack', Content: str },
     7             success: function (data) {
     8                 $("#div").html(data.ok);
     9             }
    10         });
    11     });
    复制代码

      3)、后端代码:

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

     

    本文作者:Love In Winter
    本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/15561691.html
    版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
    声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
    扫一扫,支付宝打赏 扫一扫,微信打赏
  • 相关阅读:
    html语法规范
    html页面基本结构
    HTML头部结构详解
    文件路径中 / 和 ./ 和 ../的区别
    实体符号
    利用JS修改style属性和添加元素类名(important)
    Less
    Eureka自我保护计算
    Eureka元数据
    EurekaServer源码分析
  • 原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/15561691.html
Copyright © 2011-2022 走看看