【问题标题】:What are the implications of allowing or denying GET requests from clients?允许或拒绝来自客户端的 GET 请求意味着什么?
【发布时间】:2016-03-29 19:33:06
【问题描述】:

对于 ASP.NET Web API 2 服务甚至是独立的 MVC 系统,由于我处理错误的方式等原因,我更喜欢在我的控制器中使用 JsonResult 函数:

public class BaseController : Controller
{
    // This controller is where functionality common to all
    // controllers (such as error reporting goes. It's also good for avoiding
    // code repetition as in the case of the next function

    public JsonResult CreateResponse(object Data)
    {
        // send a JsonResult with the specified data
        return Json(Data, JsonRequestBehavior.AllowGet);
    }
}

public class UserController : BaseController
{
    public JsonResult Create(CreateUserViewModel Model)
    {
        try
        {
            var User = new User
            {
                Username = Model.Username,
                EmailAddress = Model.EmailAddress,
                Password = Hashing.CreateHash(Model.Password)
            };

            db.Users.Add(User);
            db.SaveChanges();

            return CreateResponse(true);
        }
        catch (Exception ex)
        {
            return CreateResponse(ex.Message);
        }
    }
}

在什么情况下我希望对 JsonRequestBehavior 进行 AllowGet 或 DenyGet?

这两门课程的含义或顾虑是什么?

【问题讨论】:

  • 有人投票 可能的答案太多,或者对于这种格式来说,好的答案太长了。请添加详细信息以缩小答案范围或隔离可以在几段中回答的问题。我当然不同意。
  • @ErikPhilips 如何在不知道可能有什么答案的情况下做到这一点?如果我确实知道哪些答案适用于这里,那么提问不是浪费时间吗?没有意义
  • @Ortund 这不是关于你,而是关于你的问题。想象一下有人问,我应该使用 c# 还是 java 来做 blah

标签: c# asp.net json asp.net-mvc


【解决方案1】:

您想要DenyGet 的主要原因只有两个(不费吹灰之力就能找到)。

首先,安全

Why are GET requests returning JSON disallowed by default?

JSON Hijacking

其次,浏览器预取

Logout: GET or POST?

在 2010 年,使用 GET 可能是一个可以接受的答案。但是今天(2013 年),浏览器会预取他们“认为”你接下来会访问的页面。

浏览器当然有可能缓存一个 ajax 调用并假设您想要相同的请求,这可能会意外注销用户,或者告诉浏览器脚本您已经注销而您还没有注销。

【讨论】:

    【解决方案2】:

    对您发现的问题进行研究,有时可能会导致 JSON 劫持。详细信息可以在这里找到 http://haacked.com/archive/2009/06/25/json-hijacking.aspx/

    【讨论】:

      猜你喜欢
      • 2012-04-18
      • 2018-03-05
      • 2021-05-24
      • 2022-01-20
      • 1970-01-01
      • 2019-04-14
      • 2023-04-02
      • 1970-01-01
      • 2020-04-15
      相关资源
      最近更新 更多