为什么默认情况下禁用 CORS?

Why is CORS Disabled by Default?

好的,首先,我完全知道我们对此有很多答案,并且有很多关于该主题的文章。我刚刚在输入之前阅读了这些答案:

Why is CORS without credentials forbidden?.
Is CORS considered bad practice?

等我的具体情况是这样的——我刚刚为我的练习项目设置了 WebAPI2,其前端是 运行 通过 gulp 浏览器同步。我不知道这些端口是如何被选中的,但是在询问这个问题之前,可以说 Web API 在端口 http://localhost:1234/ and browser-sync generates the website on http://localhost:4321/. So I hit the API via angular's $http and get the famous CORS error (API controller method does get hit), so I am guessing it's the API returning not allowed. Edit: I fixed this via installing a CORS for Web API package via NuGet (Article Here) 上是 运行,只是供以后可能需要它的人参考。

所以,我在想,如果我部署了这个,任何请求都会被拒绝,除非我遗漏了什么。还是不会因为我不明白而被拒绝?不允许 CORS 只是 MVC 时代的倒退吗?或者 APIs 有什么目的吗?

也许我只是在咆哮,但这让我很困惑。

CORS 基于从 API 返回的响应 headers。不是 API 拒绝响应请求,web 浏览器 明确不允许处理响应。 API 将正常处理请求。

在处理除 GET 之外的任何内容时,CORS 还需要先向 API 发出 "preflight" 请求,以确保允许后续请求。 Web API nuget 包提供的发送 headers 中的这一点。

出于安全考虑,CORS 默认处于关闭状态。