首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Credentials标志为'true',但'Access-Control-Allow-Credentials

Credentials标志为'true',但'Access-Control-Allow-Credentials
EN

Stack Overflow用户
提问于 2015-10-22 05:21:55
回答 4查看 26.9K关注 0票数 9

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

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

代码语言:javascript
复制
       var cors = new EnableCorsAttribute("http://localhost:221", "*","GET,PUT,POST,DELETE");
       config.EnableCors(cors);

使用此AngularJS

代码语言:javascript
复制
        $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中

代码语言:javascript
复制
   <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="http://localhost:221" />
  </customHeaders>
</httpProtocol>

我得到了这个错误信息

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

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

EN

回答 4

Stack Overflow用户

发布于 2016-12-12 20:35:17

对于谁,谁使用WebApiConfig.cs:

代码语言:javascript
复制
config.EnableCors(new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true }); 
票数 28
EN

Stack Overflow用户

发布于 2016-01-08 02:28:35

头部被添加了两次,一次由代码添加,另一次由web.config添加。CORS支持用于允许为CORS目的添加报头。配置自定义标头还会将响应标头添加到任何请求,因此您可能需要删除配置设置。

代码语言:javascript
复制
var cors = new EnableCorsAttribute..

<customHeaders>
    <add name="Access-Control-Allow-Origin" value="http://localhost:221" />
</customHeaders>

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

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

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

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

希望能有所帮助。

票数 10
EN

Stack Overflow用户

发布于 2016-08-17 02:14:13

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

代码语言:javascript
复制
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    app.UseCors(builder =>
        builder
        .AllowCredentials()
        .WithOrigins("http://localhost:3000"));
    ...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33269488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档