【问题标题】:Credentials flag is 'true', but the 'Access-Control-Allow-CredentialsCredentials 标志为“true”,但“Access-Control-Allow-Credentials”
【发布时间】:2016-01-21 01:13:17
【问题描述】:

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

凭据标志为“true”,但“Access-Control-Allow-Credentials”标头为“”。必须为“true”才能允许凭据。 Origin '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

   <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 并得到一个错误,说它被添加了多次。我已经阅读了很多文章,似乎无法找到正确的答案。我正在使用谷歌浏览器。非常感谢您的帮助,因为我现在正在拔头发。

【问题讨论】:

标签: c# asp.net angularjs asp.net-web-api


【解决方案1】:

尝试这里列出的预检请求方法:

enabling cross-origin resource sharing on IIS7

并使用 Chrome 扩展 Postman 或 Fiddler 来更轻松地调试 CORS。我敢打赌,您将两次添加标头,但是如果没有您的整个代码,则很难调试。 (哎呀,即使使用代码也很难调试 CORS)。

在我看来,您不应该同时拥有 web.config 设置和全局 EnableCors() 属性 - 这会导致双打。

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

【讨论】:

    【解决方案2】:

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

    var cors = new EnableCorsAttribute..
    
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://localhost:221" />
    </customHeaders>
    

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

    使用参数withCredentials: true 进行AJAX 调用时,响应标头应具有Access-Control-Allow-Credentials = true。您需要使用 SupportsCredentials = true 为 CORS 属性添加代码。否则你会得到错误 "凭据标志为 'true',但 'Access-Control-Allow-Credentials 为 ''"

    有关 withCredential 参数和响应标头的更多信息,请参阅这篇文章:

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

    希望对你有帮助。

    【讨论】:

    • 关于 SupportsCredentials = true 的好评论。对于谁,谁使用 WebApiConfig.cs:config.EnableCors(new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true });
    • @RomanO 谢谢,这是唯一对我有用的解决方案
    • @RomanO,您应该提出您的答案作为问题的答案。经过数小时的搜索,这是唯一对我有用的解决方案。
    • 好的,伙计们,添加为答案。
    【解决方案3】:

    我在尝试从 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"));
        ...
    }
    

    【讨论】:

      【解决方案4】:

      为谁使用WebApiConfig.cs:

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

      【讨论】:

        猜你喜欢
        • 2022-01-17
        • 2022-10-13
        • 2015-04-14
        • 2018-03-27
        • 2019-02-08
        • 2017-10-02
        • 2019-12-14
        • 2014-09-01
        • 2022-01-13
        相关资源
        最近更新 更多