在一个asp.net 的项目中,前端通过ajax将富文本中的文字内容post到服务端的一个ashx中,在ashx中尝试读取参数值时,
结果报错:“从客户端中检测到有潜在危险的 Request.Form 值”
由于在asp.net中,Request提交时出现有html代码字符串时,程序系统会认为其具有潜在危险的值。会报出“从客户端 中检测到有潜在危险的Request.Form值”这样的Error。
而富文本中的内容是包含html代码的,所以...
前端代码:
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); }效果图:
前端代码:
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);效果图:
打开当前.aspx页面,页头加上代码:validateRequest=”false”,如:
<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="false" CodeFile="default.aspx.cs" Inherits="default" %>该方法不推荐,还有一种修改web.config配置文件的方法,强烈不推荐,就不写在这里了;
1)、针对某个实体类的单个字段设置 [AllowHtml] ,这样提交的时候,系统就会放过该字段。
2)、前端代码:
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); } }); });3)、后端代码:
public class NewInfo { public string Title { get; set; } [AllowHtml] public string Content { get; set; } }该文只是浅显的总结一下,其中涉及的xss方面,没有详细考虑,欢迎指正!
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼原文地址:https://www.cnblogs.com/willingtolove/p/10923895.html ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲