Dlaczego jsonrequestbehavior jest potrzebny?

Dlaczego Json Request Behavior jest potrzebne?

Jeśli chcę ograniczyć HttpGet żądania do mojej akcji, mogę udekorować akcję atrybutem [HttpPost]

Przykład:

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

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

Dlaczego [HttpPost] nie wystarcza?
Dlaczego framework "buguje" nas JsonRequestBehavior.AllowGet dla każdego JsonResult, który mamy. Jeśli chcę odrzucić żądania get, dodam atrybut HttpPost.

Author: gdoron, 2011-12-11

5 answers

Domyślnie MVC to DenyGet, aby chronić Cię przed bardzo specyficznym atakiem obejmującym żądania JSON w celu poprawy liklihood, że konsekwencje zezwalania na ekspozycję HTTP GET są brane pod uwagę przed dopuszczeniem do ich wystąpienia.

Jest to sprzeczne z czasem, kiedy może być za późno.

Uwaga: Jeśli twoja metoda akcji nie zwraca wrażliwych danych, to powinno być bezpiecznie zezwolić na get.

Czytaj dalej mój Wrox ASP.NET MVC3 book

By default, the ASP.NET MVC framework nie pozwala na reagowanie na żądanie HTTP GET z ładunkiem JSON. Jeśli chcesz wysłać JSON w odpowiedzi na GET, musisz wyraźnie zezwolić na zachowanie przez korzystanie z JsonRequestBehavior.AllowGet jako drugi parametr do Json metoda. Istnieje jednak szansa, że złośliwy użytkownik uzyska dostęp do ładunek JSON poprzez proces znany jako JSON Hijacking. Nie. chcesz zwrócić poufne informacje za pomocą JSON w żądaniu GET. Na więcej szczegółów, zobacz post Phila na http://haacked.com/archive/2009/06/24/json-hijacking.aspx / lub to tak post.

Haack, Phil (2011). Professional ASP.NET MVC 3 (programista Wrox do Programator) (Lokalizacje Kindle 6014-6020). Wrox. Kindle Edition.

Powiązane pytanie o StackOverflow

W przypadku większości najnowszych przeglądarek (począwszy od Firefoksa 21, Chrome 27 lub IE 10) nie jest to już Luka.

 247
Author: danludwig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-01 20:37:32

Aby ułatwić sobie życie, możesz również utworzyć actionfilterattribute

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);
    }
}

I użyj go na swojej akcji

[AllowJsonGet]
public JsonResult MyAjaxAction()
{
    return Json("this is my test");
}
 51
Author: Arjen de Mooij,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-29 08:48:13

Domyślnie Jsonresult "Deny get"

Załóżmy, że mamy metodę jak poniżej

  [HttpPost]
 public JsonResult amc(){}

Domyślnie jest to "Deny Get".

W poniższej metodzie

public JsonResult amc(){}

Gdy potrzebujesz allowget lub użyć get, musimy użyć JsonRequestBehavior.AllowGet.

public JsonResult amc()
{
 return Json(new Modle.JsonResponseData { Status = flag, Message = msg, Html = html }, JsonRequestBehavior.AllowGet);
}
 6
Author: Deepakmahajan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-16 12:21:06

Poprawa po odpowiedzi @Arjen de Mooij trochę poprzez dokonywanie AllowJsonGetAttribute ma zastosowanie do kontrolerów mvc (nie tylko indywidualnych metod działania):

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);
    }
}
 4
Author: xDisruptor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-06 00:28:15

Nie potrzebujesz tego.

Jeśli twoja akcja ma atrybut HttpPost, to nie musisz zawracać sobie głowy ustawieniem JsonRequestBehavior i używać przeciążenia bez niego. Istnieje przeciążenie dla każdej metody Bez JsonRequestBehavior enum. Oto one:

Bez 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);

Z 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);
 1
Author: CodingYoshi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-10 22:54:41