凭据标志为“true”,但是“Access-Control-Allow-Credentials”

我正在尝试从AngularJS页面连接到ASP.NET Web-API Web服务,我得到以下内容

凭据标志为“true”,但“Access-Control-Allow-Credentials”标头为“”。 允许凭据必须为“true”。 因此不允许来源’ http:// localhost:221 ‘访问。

var cors = new EnableCorsAttribute("http://localhost:221", "*","GET,PUT,POST,DELETE"); config.EnableCors(cors); 

使用此AngularJS

  $http({ method: 'GET', url: 'http://localhost:1980/api/investors/62632', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, withCredentials: true // withCredentials: true, }).then(function onUserComplete(response) { // this callback will be called asynchronously // when the response is available }, function onError(response) { // called asynchronously if an error occurs // or server returns response with an error status. 

阅读了很多文章后,我将其添加到web.config中

       

我收到此错误消息

‘Access-Control-Allow-Origin’标头包含多个值localhost:221,localhost:221,但只允许一个。 原因localhost:221因此不允许访问。

这真的没有任何意义,因为我已经添加了一次,它没有找到它,但我将它添加到web.config并得到一个错误说它被添加了多次。 我看过很多文章,似乎找不到合适的答案。 我正在使用Google Chrome。 非常感谢你的帮助,因为我现在正在拔头发。

对谁,谁使用WebApiConfig.cs:

 config.EnableCors(new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true }); 

标题由代码添加两次,另一个由web.config添加。 CORS支持用于允许为CORS目的添加标头。 配置自定义标头还会向任何请求添加响应标头,因此您可能希望删除配置设置。

 var cors = new EnableCorsAttribute..    

由于这两个区域都添加了两次相同的原点,因此您可以在标题上获得多个值。

使用参数withCredentials: true进行AJAX调用时,响应头应具有Access-Control-Allow-Credentials = true 。 您需要使用SupportsCredentials = true为CORS属性通过代码添加它。 否则,您将收到错误“Credentials flag is’true’,但’Access-Control-Allow-Credentials is””

有关更多信息,请参阅withCredential参数和响应标头,请参阅本文:

http://www.ozkary.com/2015/12/api-oauth-token-access-control-allow-credentials.html

希望能帮助到你。

我试图在angular2应用程序上点击.net核心上的webapi时遇到了这个问题。 我必须在Startup.cs的Configure方法中将allowCredentials()添加到cors配置中,如下所示。

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... app.UseCors(builder => builder .AllowCredentials() .WithOrigins("http://localhost:3000")); ... } 

尝试此处概述的方法用于预检请求:

在IIS7上启用跨源资源共享

并使用Chrome扩展程序Postman或Fiddler来更轻松地调试CORS。 我愿意打赌你要添加两次标题,但没有你的整个代码,很难调试。 (哎呀,即使使用代码,CORS也难以调试)。

对我来说,似乎你不应该同时拥有web.config设置以及全局的EnableCors()属性 – 这会导致双打。

您似乎没有在服务器端的任何位置添加Access-Control-Allow-Credentials ,但可能会由AllowCors属性添加,我不确定。 (我偏爱自己在OWIN处理CORS)