JsonRequestBehavior가 필요한 이유는 무엇입니까? [HttpPost]속성을 사용 하여 작업을 꾸밀

Json Request Behavior필요한가요?

HttpGet요청을 내 작업 으로 제한 하려면 [HttpPost]속성을 사용 하여 작업을 꾸밀 수 있습니다

예:

[HttpPost]
public JsonResult Foo()
{
    return Json("Secrets");
}

// Instead of:
public JsonResult Foo()
{
    return Json("Secrets", JsonRequestBehavior.AllowGet);
}

[HttpPost]충분 하지 않습니까?
왜 프레임 워크 가 우리가 가진 JsonRequestBehavior.AllowGet모든 JsonResult것을 “버그”하는가 . 요청을 거부하려면 HttpPost속성을 추가 합니다.



답변

MVC는 기본적으로 DenyGetJSON 요청과 관련된 매우 구체적인 공격으로부터 사용자를 보호하여 HTTP GET노출 허용의 의미 가 발생하기 전에 고려 되는 가능성을 개선합니다 .

나중에 너무 늦을 수 있습니다.

참고 : 조치 방법이 민감한 데이터를 리턴하지 않으면 가져 오기를 허용하는 것이 안전해야합니다.

Wrox ASP.NET MVC3 책에서 더 읽을 거리

기본적으로 ASP.NET MVC 프레임 워크에서는 JSON 페이로드로 HTTP GET 요청에 응답 할 수 없습니다. GET에 대한 응답으로 JSON을 보내야하는 경우 Json 메소드의 두 번째 매개 변수로 JsonRequestBehavior.AllowGet을 사용하여 동작을 명시 적으로 허용해야합니다. 그러나 악의적 인 사용자가 JSON 하이재킹이라는 프로세스를 통해 JSON 페이로드에 액세스 할 가능성이 있습니다. GET 요청에서 JSON을 사용하여 민감한 정보를 반환하지 않으려 고합니다. 자세한 내용은 Phil의 게시물 ( http://haacked.com/archive/2009/06/24/json-hijacking.aspx/) 또는 이 SO 게시물을 참조하십시오
.

Haack, Phil (2011). 전문가 용 ASP.NET MVC 3 (프로그래머 대 프로그래머) (Kindle Locations 6014-6020). 약 킨들 에디션.

관련 StackOverflow 질문

Firefox 21, Chrome 27 또는 IE 10부터 시작하는 최신 브라우저에서는 더 이상 취약점이 아닙니다.


답변

더 쉽게 자신을 위해 액션 필터 속성을 만들 수도 있습니다

public class AllowJsonGetAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var jsonResult = filterContext.Result as JsonResult;

        if (jsonResult == null)
            throw new ArgumentException("Action does not return a JsonResult,
                                                   attribute AllowJsonGet is not allowed");

        jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

        base.OnResultExecuting(filterContext);
    }
}

그리고 당신의 행동에 그것을 사용

[AllowJsonGet]
public JsonResult MyAjaxAction()
{
    return Json("this is my test");
}

답변

기본적으로 Jsonresult “거부”

아래와 같은 방법이 있다고 가정 해보십시오.

  [HttpPost]
 public JsonResult amc(){}

기본적으로 “거부”입니다.

아래 방법에서

public JsonResult amc(){}

get을 허용하거나 사용해야 할 경우 JsonRequestBehavior.AllowGet을 사용해야합니다.

public JsonResult amc()
{
 return Json(new Modle.JsonResponseData { Status = flag, Message = msg, Html = html }, JsonRequestBehavior.AllowGet);
}

답변

AllowJsonGetAttribute를 개별 액션 메소드뿐만 아니라 mvc 컨트롤러에 적용하여 @Arjen de Mooij의 답변을 약간 향상시킵니다.

using System.Web.Mvc;
public sealed class AllowJsonGetAttribute : ActionFilterAttribute, IActionFilter
{
    void IActionFilter.OnActionExecuted(ActionExecutedContext context)
    {
        var jsonResult = context.Result as JsonResult;
        if (jsonResult == null) return;

        jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var jsonResult = filterContext.Result as JsonResult;
        if (jsonResult == null) return;

        jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        base.OnResultExecuting(filterContext);
    }
}

답변

필요하지 않습니다.

작업에 HttpPost속성 이있는 경우에는 설정을 방해 JsonRequestBehavior하지 않고 오버로드를 사용하지 않아도됩니다. JsonRequestBehavior열거 형이 없는 각 메소드에는 과부하가 있습니다 . 여기 있습니다:

JsonRequestBehavior없이

protected internal JsonResult Json(object data);
protected internal JsonResult Json(object data, string contentType);
protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding);

JsonRequestBehavior 사용

protected internal JsonResult Json(object data, JsonRequestBehavior behavior);
protected internal JsonResult Json(object data, string contentType,
                                   JsonRequestBehavior behavior);
protected internal virtual JsonResult Json(object data, string contentType,
    Encoding contentEncoding, JsonRequestBehavior behavior);