1. 程式人生 > >"從客戶端中檢測到有潛在危險的 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     }

 #寫在最後

 該文只是淺顯的總結一下,其中涉及的xss方面,沒有詳細考慮,歡迎指正!

——————————————————————————————————————————