Asp.net web api ';访问控制允许原点';标头包含多个值

Asp.net web api ';访问控制允许原点';标头包含多个值,asp.net-web-api,cors,angularjs-http,Asp.net Web Api,Cors,Angularjs Http,我在客户端使用AngularJS$http访问服务器端ASP.NET Web API应用程序的端点。由于客户端与服务器托管在不同的域上,因此我需要CORS。它适用于$http.post(url,数据)。但是,只要我对用户进行身份验证并通过$http.get(url)发出请求,我就会收到消息 The 'Access-Control-Allow-Origin' header contains multiple values 'http://127.0.0.1:9000, http://127.0.0

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

The 'Access-Control-Allow-Origin' header contains multiple values 'http://127.0.0.1:9000, http://127.0.0.1:9000', but only one is allowed. Origin 'http://127.0.0.1:9000' is therefore not allowed access. “访问控制允许来源”标题包含多个值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似乎没有问题。但是哪里不对呢?

我补充道

config.EnableCors(新的EnableCorsAttribute(Properties.Settings.Default.Cors,“,”)

以及

app.UseCors(CorsOptions.AllowAll)


在服务器上。这将导致两个标题条目。只要使用后一个,它就会工作。

Apache服务器:

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

Header add Access-Control-Allow-Origin: * 
Header add Access-Control-Allow-Origin "*" 
您也可能在文件夹中有一个文件“.htaccess”,而另一个文件“.htaccess”不在文件夹中,例如

/ 
- .htaccess 
- public_html / .htaccess (problem here)
在您的情况下,星号不是“*”而是ip(
http://127.0.0.1:9000
)您授予数据服务权限的服务器

ASP.NET:

检查代码中是否没有重复的“访问控制允许来源”

开发人员工具:

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


我正在使用Cors 5.1.0.0,在经历了很多头痛之后,我发现问题重复了 访问控制允许来源&访问控制允许来自服务器的标头

从WebApiConfig.cs文件中删除了
config.EnableCors()
,只需在控制器类上设置
[EnableCors(“*”,“*”,“*”,“*”)
属性


查看更多详细信息。

实际上,您不能设置多个标题
访问控制允许源代码
(或者至少它不会在所有浏览器中工作)。相反,您可以有条件地设置环境变量,然后在
标题
指令中使用它:

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)

请记住启用
setenif
模块

文件:


顺便说一句,
%{ORIGIN\u SUB\u DOMAIN}e
中的
}e
不是打字错误。这是在
头指令中使用环境变量的方式。

刚刚在nodejs服务器上出现了这个问题

下面是我如何修复它的。

我通过
nginx代理运行我的节点服务器
,我将nginx和
node
设置为
允许跨域请求
,但它不喜欢这样,所以我将其从nginx中删除并保留在node中,一切正常。

我们遇到了这个问题,因为我们根据最佳实践(例如)设置了CORS并且在web.config中还有一个自定义标题

删除web.config条目,一切正常


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

我也有两个OWIN和我的WebAPI,它们显然都需要单独启用CORS,这反过来又创建了
“Access-Control-Allow-Origin”标题包含多个值e> 错误

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

<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>
<remove name="OPTIONSVerbHandler" />

希望这对其他人有所帮助。

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

我的理解是,如果您希望在应用程序范围内使用它,那么只需在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;

如果您在IIS中,则需要在web.config中激活CORS,然后不需要启用应用程序中的Start/WebApiConfig.cs Register方法

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

// Enable CORS
//EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
//config.EnableCors(cors);
并在web.config中写入:

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


如果您实际设置了
访问控制允许源文件
标题的多个值,当然也可能发生这种情况。例如,大多数主要浏览器都不支持逗号分隔的值列表。请注意,在不使用“*”的情况下

例如,您可以使用如下标题在Chrome中获得该错误:

访问控制允许原点:http://test.mysite.com, http://test2.mysite.com

这是在Chrome版本64.0.3282.186(官方版本)(64位)

请注意,如果您考虑这是因为CDN,并且您使用Akamai,您可能需要注意
Vary:Origin
,这是许多人建议的解决此问题的方法


您可能必须使用“缓存ID修改”响应行为来更改缓存密钥的构建方式。此外,我也遇到了同样的问题,我就是这样解决的:

在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
<?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>
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

<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"
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
<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>