122

我在客户端使用 AngularJS $http 来访问服务器端的 ASP.NET Web API 应用程序的端点。由于客户端与服务器托管在不同的域上,因此我需要 CORS。它适用于 $http.post(url, data)。但只要我验证用户身份并通过 $http.get(url) 发出请求,我就会收到消息

“Access-Control-Allow-Origin”标头包含多个值“http://127.0.0.1:9000, http://127.0.0.1:9000”,但只允许一个。因此不允许访问源“http://127.0.0.1:9000”。

Fiddler 告诉我,在成功的选项请求后,get 请求中确实有两个标头条目。我在哪里做错了什么?

更新

当我使用 jQuery $.get 而不是 $http.get 时,会出现相同的错误消息。所以这对 AngularJS 来说似乎没有问题。但是哪里错了?

4

16 回答 16

57

我添加了

config.EnableCors(new EnableCorsAttribute(Properties.Settings.Default.Cors, "", ""))

app.UseCors(CorsOptions.AllowAll);

在服务器上。这会产生两个标题条目。只需使用后一个即可。

于 2014-03-12T07:34:07.013 回答
56

我们遇到了这个问题,因为我们已经根据最佳实践设置了 CORS(例如http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api)并且<add name="Access-Control-Allow-Origin" value="*"/>在 web.config 中 还有一个自定义标头。

删除 web.config 条目,一切都很好。

与@mww 的回答相反,我们仍然EnableCors()在 WebApiConfig.cs 和EnableCorsAttribute控制器上有一个。当我们拿出一个或另一个时,我们遇到了其他问题。

于 2015-01-12T22:16:32.847 回答
45

我正在使用 Cors 5.1.0.0,在头疼之后,我发现问题是服务器上的 Access-Control-Allow-Origin 和 Access-Control-Allow-Header 标头重复

config.EnableCors()从 WebApiConfig.cs 文件中删除,只需在[EnableCors("*","*","*")]Controller 类上设置属性

查看这篇文章了解更多详情。

于 2014-05-24T02:24:05.110 回答
19

添加注册 WebApiConfig

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

或 web.config

<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>  
</httpProtocol>

但不是两者

于 2019-03-08T15:28:20.470 回答
9

我也同时拥有 OWIN 和我的 WebAPI,它们显然都需要单独启用 CORS,这反过来又产生了'Access-Control-Allow-Origin' header contains multiple values错误。

我最终删除了所有启用 CORS 的代码,然后将以下内容添加到system.webServer我的 Web.Config 的节点中:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="https://stethio.azurewebsites.net" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Authorization" />
  </customHeaders>
</httpProtocol>

这样做满足了 OWIN(允许登录)和 WebAPI(允许 API 调用)的 CORS 要求,但它产生了一个新问题:OPTIONS在我的 API 调用的预检期间找不到方法。解决方法很简单——我只需要从handlers我的 Web.Config 节点中删除以下内容:

<remove name="OPTIONSVerbHandler" />

希望这可以帮助某人。

于 2015-08-05T22:20:40.520 回答
8

阿帕奇服务器:

我花了同样的钱,但这是因为我的文件中没有引号 (") 星号提供了对服务器的访问,例如'.htaccess.':

Header add Access-Control-Allow-Origin: * 
Header add Access-Control-Allow-Origin "*" 

您也可能在一个文件夹中有一个文件“.htaccess”,而另一个“.htaccess”在其中,例如

/ 
- .htaccess 
- public_html / .htaccess (problem here)

在您的情况下,星号将是http://127.0.0.1:9000您授予提供数据许可的 ip ( ) 服务器,而不是星号。

ASP.NET:

检查您的代码中是否没有“Access-Control-Allow-Origin”重复项。

开发者工具:

使用 Chrome,您可以验证您的请求标头。按 F12 键并转到“网络”选项卡,现在运行 AJAX 请求并将出现在列表中,单击并提供所有信息。

访问控制允许来源:*

于 2014-03-22T00:41:32.300 回答
8

实际上你不能设置多个标题Access-Control-Allow-Origin(或者至少它不会在所有浏览器中工作)。相反,您可以有条件地设置环境变量,然后在Header指令中使用它:

SetEnvIf Origin "^(https?://localhost|https://[a-z]+\.my\.base\.domain)$" ORIGIN_SUB_DOMAIN=$1
Header set Access-Control-Allow-Origin: "%{ORIGIN_SUB_DOMAIN}e" env=ORIGIN_SUB_DOMAIN

Origin所以在这个例子中,只有当请求头与 RegExp 匹配时才会添加响应头:(^(https?://localhost|https://[a-z]+\.my\.base\.domain)$它基本上意味着通过 HTTP 或 HTTPS 的 localhost 和通过 HTTPS 的 *.my.base.domain)。

记得启用setenvif模块。

文件:

顺便提一句。}ein%{ORIGIN_SUB_DOMAIN}e不是错字。这是您在Header指令中使用环境变量的方式。

于 2014-06-02T16:11:00.697 回答
4

当您在多个位置配置 Cors 选项时会发生这种情况。就我而言,我在控制器级别以及 Startup.Auth.cs/ConfigureAuth 中都有它。

我的理解是,如果你想要它在应用程序范围内,那么只需像这样在 Startup.Auth.cs/ConfigureAuth 下配置它......你需要参考 Microsoft.Owin.Cors

public void ConfigureAuth(IAppBuilder app)
        {
          app.UseCors(CorsOptions.AllowAll);

如果您宁愿将其保留在控制器级别,那么您可以只在控制器级别插入。

[EnableCors("http://localhost:24589", "*", "*")]
    public class ProductsController : ApiController
    {
        ProductRepository _prodRepo;
于 2015-08-09T15:17:19.930 回答
4

如果您在 IIS 中需要在 web.config 中激活 CORS,则无需在 App_Start/WebApiConfig.cs 中启用注册方法

我的解决方案是,在这里评论这些行:

// Enable CORS
//EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
//config.EnableCors(cors);

并在 web.config 中写入:

<system.webServer>
  <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
</httpProtocol>

于 2017-10-26T02:00:05.467 回答
2

刚刚在nodejs服务器上遇到了这个问题。

这是我修复它的方法。
我通过 a 运行我的节点服务器nginx proxy,并将 nginx 设置node为两者allow cross domain requests,但它不喜欢这样,所以我将它从 nginx 中删除并将其留在节点中,一切都很好。

于 2014-08-08T02:47:10.207 回答
2

当然,如果您实际上将Access-Control-Allow-Origin标头设置为具有多个值,这当然也可能发生 - 例如,逗号分隔的值列表,这在 RFC中是受支持的,但大多数主要浏览器实际上并不支持。请注意, RFC 讨论了如何在不使用“*”的情况下允许多个域。

例如,您可以通过使用如下标头在 Chrome 中获取该错误:

Access-Control-Allow-Origin: http://test.mysite.com, http://test2.mysite.com

这是在Chrome Version 64.0.3282.186 (Official Build) (64-bit)

请注意,如果您因为 CDN 而考虑这一点,并且您使用 Akamai,您可能需要注意,如果您使用 ,Akamai 不会在服务器上缓存,这 Vary:Origin是许多人建议解决此问题的方式。

您可能必须使用“缓存 ID 修改”响应行为来更改缓存键的构建方式。此相关 StackOverflow 问题中有关此问题的更多详细信息

于 2018-03-13T17:38:01.177 回答
2

如此愚蠢和简单:

Header always set Access-Control-Allow-Origin *在我的 Apache 配置文件中有两次时间时,我发生了这个问题。一次使用VirtualHost标签和一次在Limit标签内:

<VirtualHost localhost:80>
  ...
  Header set Access-Control-Allow-Origin: *
  ...
  <Limit OPTIONS>
    ...
    Header set Access-Control-Allow-Origin: *
    ...
  </Limit>
</VirtualHost>

删除一个条目解决了该问题。

我猜在原始帖子中会出现两次:

Header set Access-Control-Allow-Origin: "http://127.0.0.1:9000"
于 2019-03-06T16:12:04.863 回答
1

“Access-Control-Allow-Origin”标头包含多个值

当我收到这个错误时,我花了很多时间寻找解决方案,但没有任何效果,最后我找到了非常简单的解决方案。当“Access-Control-Allow-Origin”标头多次添加到您的响应中时,会发生此错误,请检查您的 apache.conf 或 httpd.conf(Apache 服务器)、服务器端脚本,并从这些文件中删除不需要的条目标头.

于 2020-07-11T11:57:16.937 回答
0

我遇到了同样的问题,这就是我为解决它所做的:

在 WebApi 服务的 Global.asax 中,我编写了以下代码:

Sub Application_BeginRequest()
        Dim currentRequest = HttpContext.Current.Request
        Dim currentResponse = HttpContext.Current.Response

        Dim currentOriginValue As String = String.Empty
        Dim currentHostValue As String = String.Empty

        Dim currentRequestOrigin = currentRequest.Headers("Origin")
        Dim currentRequestHost = currentRequest.Headers("Host")

        Dim currentRequestHeaders = currentRequest.Headers("Access-Control-Request-Headers")
        Dim currentRequestMethod = currentRequest.Headers("Access-Control-Request-Method")

        If currentRequestOrigin IsNot Nothing Then
            currentOriginValue = currentRequestOrigin
        End If

        If currentRequest.Path.ToLower().IndexOf("token") > -1 Or Request.HttpMethod = "OPTIONS" Then
            currentResponse.Headers.Remove("Access-Control-Allow-Origin")
            currentResponse.AppendHeader("Access-Control-Allow-Origin", "*")
        End If

        For Each key In Request.Headers.AllKeys
            If key = "Origin" AndAlso Request.HttpMethod = "OPTIONS" Then
                currentResponse.AppendHeader("Access-Control-Allow-Credentials", "true")
                currentResponse.AppendHeader("Access-Control-Allow-Methods", currentRequestMethod)
                currentResponse.AppendHeader("Access-Control-Allow-Headers", If(currentRequestHeaders, "GET,POST,PUT,DELETE,OPTIONS"))
                currentResponse.StatusCode = 200
                currentResponse.End()
            End If
        Next

    End Sub

此处此代码仅允许飞行前和令牌请求在响应中添加“Access-Control-Allow-Origin”,否则我不会添加它。

这是我关于实现的博客:https ://ibhowmick.wordpress.com/2018/09/21/cross-domain-token-based-authentication-with-web-api2-and-jquery-angular-5-angular- 6/

于 2018-10-04T16:07:24.607 回答
0

对于那些使用 IIS 和 php 的人,在 IIS 上它服务器端更新 web.config 文件它的根目录(wwwroot)并添加这个

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
        <httpProtocol>
            <customHeaders>
                <add name="Control-Allow-Origin" value="*"/>
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

重新启动 IIS 服务器后,在 RUN 中键入 IISReset 并输入

于 2018-10-15T06:56:28.463 回答
0

这是另一个与上述示例类似的实例,您可能只有一个配置文件定义 CORS 的位置: IIS 服务器上的两个 web.config 文件位于不同目录的路径上,其中一个隐藏在虚拟目录中。为了解决这个问题,我删除了根级别的配置文件,因为路径使用的是虚拟目录中的配置文件。必须选择其中之一。

URL called:  'https://example.com/foo/bar'
                     ^              ^
      CORS config file in root      virtual directory with another CORS config file
          deleted this config             other sites using this

于 2019-02-07T21:41:38.107 回答