2011-06-08 69 views
2

我有一個接受GET請求並返回JSON對象(不是數組)的JSON方法。我知道JSON劫持及其影響。我讀過Phil Haack post。問題是該方法在GET和POST的98%時間內工作。其他時間我記錄這個錯誤:是什麼觸發MVC JSON獲取阻止 - 間歇性工作

This request has been blocked because sensitive information could be disclosed to 
third party web sites when this is used in a GET request. To allow GET requests, set 
JsonRequestBehavior to AllowGet. 

我的方法很簡單,取一個整數參數...

[Authorize] 
public ActionResult MyMediaJSON(int? id) { 
    <get data & return result> 
} 

什麼樣的條件觸發消息?我在調試時應該看到什麼?

+1

會因爲您正在使用'ActionResult'而不是'JsonResult'和一個奇怪的算法問題,它會搞砸嗎?你能記錄你一直在傳遞什麼嗎?運行** Fiddler **並測試,一旦出現錯誤,請檢查差異。 – balexandre 2011-06-08 14:22:53

+0

@balexandre - 有趣的是你指出。由於我在複製時遇到了麻煩,因此我將不得不在結果類型中進行更改,然後觀察間歇性錯誤。我懷疑這個請求在有機會返回之前就被阻止了,但我會給出一個答案。 – EBarr 2011-06-08 18:24:04

回答

3

我剛剛看了看MVC的源代碼,它並沒有說明你在說什麼。

對我來說,它看起來像JsonRequestBehavior.DenyGet默認情況下用於所有JSON結果。因此,每次嘗試使用GET請求從控制器返回JSON時(無需指定JsonRequestBehavior.AllowGet),都應該收到錯誤消息。

的實際控制是在JsonResult.ExecuteResult完成,看起來像:

if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && 
    String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { 
    throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed); 
} 

What conditions trigger the message? What should look for as I debug this?

那些獲得通過GET,如果沒有指定JsonRequestBehavior.AllowGet返回JsonResult調用的任何行動。 (控制器中的Json方法使用JsonResult

+0

感謝您的挖掘。我回去挖掘MVC源代碼,你是對的。在我的情況下,我看到一個間歇性問題b/c我的框架正在檢查會話超時,並在ajax請求的情況下返回一個JSON結果。我的普通json結果允許得到,但會話超時信號缺少該標誌。再次感謝。 – EBarr 2011-06-13 15:26:02

相關問題