我在客户端使用AngularJS $ http访问服务器端ASP.NET Web API应用程序的端点。由于客户端作为服务器托管在不同的域上,因此我需要CORS。它适用于$ http.post(url,data)。但是只要我通过$ 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.
Fiddler告诉我,在成功的选项请求之后,get请求中确实有两个头条目。我做错什么地方在哪里?
更新
当我使用jQuery $ .get而不是$ http.get时,会出现相同的错误消息。所以AngularJS似乎没有问题。但哪里错了?
我补充道
config.EnableCors(new EnableCorsAttribute(Properties.Settings.Default.Cors, "", ""))
以及
app.UseCors(CorsOptions.AllowAll);
在服务器上。这导致两个标题条目。只需使用后者即可。
刚刚遇到nodejs服务器的这个问题。
这是我如何修复它。
我通过// Enable CORS
//EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
//config.EnableCors(cors);
运行我的节点服务器,我将nginx和<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
设置为两个nginx proxy
,它不是那样的,所以我从nginx中删除它并将其留在节点中,一切都很好。
当然,如果您实际上将node
标头设置为具有多个值,也可能发生这种情况 - 例如,以逗号分隔的值列表,即allow cross domain requests
,但大多数主流浏览器实际上并不支持。请注意,Access-Control-Allow-Origin
也不使用'*'。
例如,您可以使用如下标题在Chrome中获取该错误:
这是在the RFC talks about how to allow more than one domain
请注意,如果您因为CDN而考虑这一点,并且您使用Akamai,您可能需要注意Access-Control-Allow-Origin: http://test.mysite.com, http://test2.mysite.com
Chrome Version 64.0.3282.186 (Official Build) (64-bit)
,许多建议解决此问题的方式。
您可能必须使用“缓存ID修改”响应行为来更改缓存键的构建方式。更多Akamai wont cache on the server if you use
在我的Apache配置文件中有两次Vary:Origin
时,我遇到了这个问题。一旦使用details on this issue in this related StackOverflow question标签,一次在Header always set Access-Control-Allow-Origin *
标签内:
VirtualHost
删除一个条目解决了该问题。
我猜在原帖中它会有两次:
Limit
我遇到了同样的问题,这就是我解决它的方法:
在Web Api服务中,在Global.asax中我编写了以下代码:
<VirtualHost localhost:80>
...
Header set Access-Control-Allow-Origin: *
...
<Limit OPTIONS>
...
Header set Access-Control-Allow-Origin: *
...
</Limit>
</VirtualHost>
这里的代码只允许pre-flight和token请求在响应中添加“Access-Control-Allow-Origin”,否则我不会添加它。
这是我关于实施的博客:Header set Access-Control-Allow-Origin: "http://127.0.0.1:9000"
对于那些使用IIS与IIS的人,在IIS上它服务器端更新web.config文件它的根目录(wwwroot)并添加这个
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
在重新启动IIS服务器之后,在RUN中键入IISReset并输入
这是另一个类似于上面示例的实例,您可能只有一个配置文件定义CORS所在的位置:IIS服务器上的两个web.config文件位于不同目录中的路径上,其中一个隐藏在虚拟目录中。为了解决这个问题,我删除了根级配置文件,因为路径使用的是虚拟目录中的配置文件。必须选择其中一个。
https://ibhowmick.wordpress.com/2018/09/21/cross-domain-token-based-authentication-with-web-api2-and-jquery-angular-5-angular-6/
我们遇到了这个问题,因为我们根据最佳实践设置了CORS(例如http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api),并且在web.config中也有自定义头<add name="Access-Control-Allow-Origin" value="*"/>
。
删除web.config条目,一切都很好。
与@ mww的答案相反,我们仍然在WebApiConfig.cs中使用EnableCors()
,在控制器上使用EnableCorsAttribute
。当我们拿出一个或另一个时,我们遇到了其他问题。
我正在使用Cors 5.1.0.0,经过多次头痛,我发现问题是从服务器复制Access-Control-Allow-Origin和Access-Control-Allow-Header头
从WebApiConfig.cs文件中删除了config.EnableCors()
,并在Controller类上设置了[EnableCors("*","*","*")]
属性
查看this article了解更多详情。
我既有OWIN也有我的WebAPI,两者显然都需要单独启用CORS,这反过来又会产生'Access-Control-Allow-Origin' header contains multiple values
错误。
我最终删除了启用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>
为OWIN(允许登录)和WebAPI(允许API调用)执行此满足的CORS要求,但它创建了一个新问题:在我的API调用的预检期间找不到OPTIONS
方法。解决这个问题很简单 - 我只需要从handlers
节点中移除以下内容我的Web.Config:
<remove name="OPTIONSVerbHandler" />
希望这有助于某人。
实际上你不能设置多个标头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或HTTPS和* .my.base.domain上的localhost)。
请记住启用setenvif
模块。
文档:
之间。 qazxsw poi中的qazxsw poi不是拼写错误。这是你在}e
指令中使用环境变量的方式。
Apache服务器:
我花了同样的钱,但那是因为我的文件中没有引号(“)星号提供了对服务器的访问,例如'.htaccess。':
%{ORIGIN_SUB_DOMAIN}e
你也可能在另一个'.htaccess'文件夹中有一个'.htaccess'文件,例如
Header
在您的情况下,而不是'*'asterisk将是您提供服务数据的权限的ip(Header add Access-Control-Allow-Origin: *
Header add Access-Control-Allow-Origin "*"
)服务器。
asp.net:
检查代码中是否存在“Access-Control-Allow-Origin”副本。
开发者工具:
使用Chrome,您可以验证您的请求标头。按F12键并转到“网络”选项卡,现在运行AJAX请求并显示在列表中,单击并提供所有信息。
如果在多个位置配置了Cors选项,则会发生这种情况。在我的情况下,我在控制器级别以及Startup.Auth.cs / ConfigureAuth中使用它。
我的理解是,如果你想要它的应用程序范围,那么只需在Startup.Auth.cs / ConfigureAuth下配置它...你需要参考Microsoft.Owin.Cors
/
- .htaccess
- public_html / .htaccess (problem here)
如果您宁愿将其保持在控制器级别,那么您可以只在控制器级别插入。
http://127.0.0.1:9000
添加到注册WebApiConfig
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
或者是web.config
[EnableCors("http://localhost:24589", "*", "*")]
public class ProductsController : ApiController
{
ProductRepository _prodRepo;
但不是两个
如果你在IIS中,你需要在web.config中激活CORS,那么你不需要在App_Start / WebApiConfig.cs中启用注册方法
我的解决方案是评论这里的线:
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>