我在webApi上启用了CORS
var corsAttr = new EnableCorsAttribute("http://localhost:14054", "*", "*") {SupportsCredentials = true};
config.EnableCors(corsAttr);
但是当我的网站从FF投影时,我仍然会收到错误
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:28821/Authenticate. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).1(unknown)
这就是我在chrome中获得的
XMLHttpRequest cannot load http://localhost:28821/Authenticate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:14054' is therefore not allowed access.
什么时候在Edge或IE中做同样的事情。我已经做了一堆阅读,但无法弄清楚如何阻止飞行前停止,但我想我读到某处chrome& FF还包括端口号。
无论哪种方式我都感到恶心和烦恼,有人可以告诉我我做错了什么。
修改 另一件事是这是我发布的公共端点,并且能够使用移动应用程序来消费api就好了。这是在我开始处理Web应用程序之前。
修改
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE, PATCH" />
<add name="Access-Control-Allow-Headers" value="authorization,accept,content-type,origin,cache-control,x-requested-with" />
<add name="Access-Control-Expose-Headers" value="x-sf3-api-version" />
</customHeaders>
</httpProtocol>
这是web配置,以及startup.register()中的enablecors()
它不是OAuth或身份验证逻辑,因为它可以从邮递员工作,也可以从移动设备上运行
答案 0 :(得分:0)
查看EnableCorsAttribute method,第一个属性是允许查询您网站的地址。
正如你在IE中所做的那样,这是因为访问它的页面位于http://localhost:14054
以外的其他地址。
您的代码应该如下所示:
var corsAttr = new EnableCorsAttribute("*", "*", "*") {SupportsCredentials = true};
config.EnableCors(corsAttr);
或查看ASP.NET documentation(在控制器或方法上):
[EnableCors(起源:&#34; &#34;,标题:&#34; &#34;,方法:&#34; *&#34;)]
public class TestController : ApiController
{
// Controller methods not shown...
}
编辑:
检查一个other answer I did here,可能是您的方法implmentation崩溃(可能是您的凭据逻辑,这意味着它不是CORS问题),这不会使API返回Access-Control-Allow-Origin
标头
答案 1 :(得分:0)
当我们通过AJAX调用受NTLM保护的REST端点时,我们在自己的项目中遇到了类似的问题-Chrome报告了CORS错误,但IE / Edge运行正常。
这里有一些指针可能有助于深入了解它。
我建议下载Telerik Fiddler,这样您就可以通过网络查看请求和响应。在我们的案例中,Web API实际上返回的是带有HTTP 200响应的有效响应,因此很明显,Chrome随后决定将其视为不受信任而将其阻止。在我们的例子中,我们错过了设置服务器端时需要使用的{SupportsCredentials = true}
部分。
但是,在修复了该问题之后(它开始通过我们使用xhrFields: { withCredentials: true }
的jQuery调用开始工作),我们注意到在React应用中我们使用fetch且未通过传递凭据,得到401/403。添加{ credentials: 'include' }
之后,该呼叫也可以正常工作。
希望这可以帮助某人,因为它使我们困扰了很长时间。