2012-03-16 73 views
2

由于ASP.NET MVC2,当您尝试不带附加信息返回JSON结果,你会得到一个错误:JsonRequestBehavior相当于Json.Net与Asp.Net的mvc

This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request.

现在必须设置属性JsonRequestBehavior为值AllowGet

result.JsonRequestBehavior = JsonRequestBehavior.AllowGet; 

read on a post这妨碍劫持。

我想知道是否有与Json.Net等效防止这种类型的攻击。

这里是我的代码来创建JSON结果:

protected JsonNetResult JsonNet(object data) 
    { 
    JsonNetResult result = new JsonNetResult(); 

    result.Data = data; 

    return result; 
    } 

如果你想知道我找到了JsonNetResult,here is a link

非常感谢。

回答

4

你并不需要它,因为你已经证明没有这样的测试自定义JsonNetResult。所以,你永远不会得到像一个,如果你调用与GET操作,你将与标准JsonResult得到一个异常。

如果你想,你可以在您的自定义JsonNetResult财产执行完全一样的性质。

public class JsonNetResult : ActionResult 
{ 
    public JsonNetResult() 
    { 
     SerializerSettings = new JsonSerializerSettings(); 
     JsonRequestBehavior = JsonRequestBehavior.DenyGet; 
    } 

    public JsonRequestBehavior JsonRequestBehavior { get; set; } 
    .... 

    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) 
      throw new ArgumentNullException("context"); 

     var httpMethod = context.HttpContext.Request.HttpMethod; 

     if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && 
      string.Equals(httpMethod, "GET", StringComparison.OrdinalIgnoreCase)) 
     { 
      throw new InvalidOperationException("You can't access this action with GET"); 
     } 

     ... 
    } 
} 

,如果你想明确允许这种特定的动作:

protected ActionResult JsonNet(object data) 
{ 
    JsonNetResult result = new JsonNetResult(); 
    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet; 
    result.Data = data; 
    return result; 
} 
+0

谢谢达林。对于我来说,从StackOverflow最强大的贡献者之一获得答案是我的荣幸。 – Samuel 2012-03-16 14:04:58