如何防止mvc上的json劫持

时间:2012-03-05 06:11:25

标签: ajax asp.net-mvc json

如何防止mvc上的json劫持?

[PreventHijack]
public JsonResult Check(int id)
{
   return Json(id, JsonRequestBehavior.AllowGet);
}

的javascript

$.ajax({
   url: "/Controller/Check?id=1",
   type: "post",
   success: function(data) {
   }
})

我不希望任何人通过地址栏浏览这个jsonresult。 是否有可能在这里使用antiforgerytoken?

1 个答案:

答案 0 :(得分:1)

您明确允许用户使用JsonRequestBehavior.AllowGet浏览到该网址。如果你这样做,.NET会在点击该URL时抛出错误:This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request