"從客戶端中檢測到有潛在危險的 Request.Form 值"的解決方案匯總

2020-3-22    seo達人

在一個asp.net 的項目中,前端通過ajax將富文本中的文字內(nèi)容post到服務(wù)端的一個ashx中,在ashx中嘗試讀取參數(shù)值時,

結(jié)果報錯:“從客戶端中檢測到有潛在危險的 Request.Form 值”

#事故分析
由于在asp.net中,Request提交時出現(xiàn)有html代碼字符串時,程序系統(tǒng)會認(rèn)為其具有潛在危險的值。會報出“從客戶端 中檢測到有潛在危險的Request.Form值”這樣的Error。

而富文本中的內(nèi)容是包含html代碼的,所以...

#解決方案:
1、前端對富文本字符串進行encodeURI編碼,服務(wù)端進行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方式提交,服務(wù)端從request的body中讀取數(shù)據(jù),然后反序列化,得到信息;
前端代碼:

    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);
效果圖:

#其他場景的解決方案:
1、aspx頁面,當(dāng)前頁面進行form提交
打開當(dāng)前.aspx頁面,頁頭加上代碼:validateRequest=”false”,如:

<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="false" CodeFile="default.aspx.cs" Inherits="default" %>
該方法不推薦,還有一種修改web.config配置文件的方法,強烈不推薦,就不寫在這里了;

2、在ASP.NET MVC中的解決方案
1)、針對某個實體類的單個字段設(shè)置 [AllowHtml] ,這樣提交的時候,系統(tǒng)就會放過該字段。

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; }
    }
 #寫在最后
 該文只是淺顯的總結(jié)一下,其中涉及的xss方面,沒有詳細(xì)考慮,歡迎指正!

分享本文至:

日歷

鏈接

個人資料

藍藍設(shè)計的小編 http://www.bouu.cn

存檔