IIS中的CORS使用凭证和通配符在Access-Control-Allow-Origin中发出

我inheritance了一个相当基本的网站,提供数据和处理一些套接字连接。 它使用iisnode作为桥梁运行IIS后面的NodeJS。 从“服务正常页面”的angular度来看,这一切都很好。

部分问题是与服务器的实际连接来自桌面客户端,其中内容通过不同的应用程序作为小工具加载,以及来自networking,移动设备等的潜在改变和变化的部分。即,未知数量的客户端域。

我已经设置访问控制,允许起源*,只是打开谷仓门,但现在在客户端中得到以下错误:

11:29:57.668跨域请求被阻止:相同的源策略不允许在' http://server/socket.io/?EIO = 3&transport = polling&t = 1486150196479-0 '处读取远程资源。 (原因:如果CORS头“Access-Control-Allow-Origin”为'*',则不支持凭据)。 1(未知)

我试图将访问控制允许的证书明确地设置为假(也是如此,以及完全抛弃),但我的任何尝试都没有让我过去这一点。

原始响应标题看起来像这样:

Access-Control-Allow-Credentials: false Access-Control-Allow-Headers: Origin,Content-Type,Accept Access-Control-Allow-Methods: GET,HEAD,PUT,POST,DELETE,OPTIONS Access-Control-Allow-Origin: * Cache-Control: no-cache Content-Encoding: gzip Content-Length: 969 Content-Type: text/html Date: Fri, 03 Feb 2017 19:30:21 GMT Server: Microsoft-IIS/8.5 Vary: Accept-Encoding X-Powered-By: ASP.NET 

在过去几天看到很多CORS网站和文章后,我似乎无法理清为什么它仍在抱怨证书 – 更具体地说,我该如何解决这个问题?

谢谢!

更新2017-02-06

客户端代码并不令人兴奋。 由于服务器是IIS后面的NodeJS,所以我真正为此做的是实现套接字连接:

 var socket = io('http://' + currentServer, {path: '/broadcast/socket.io', reconnection: false, forceNew: true}); socket.on('update message', function (data) { // do some fancy things } 

这在同一个域内工作。

我也一直在做更多的挖掘基于sideshowbarker的评论,导致我这篇文章有一些额外的步骤来添加variables,以及一些其他的东西,让那件作品。

我的applicationHost.config目前包含这个部分:

 <location path="Default Web Site"> <system.webServer> <rewrite> <allowedServerVariables> <add name="CAPTURED_ORIGIN" /> <add name="RESPONSE_Access-Control-Allow-Origin" /> </allowedServerVariables> </rewrite> </system.webServer> </location> 

和我的web.config在这里:

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Fail bad requests"> <match url="." /> <conditions> <add input="{HTTP_HOST}" negate="true" pattern="localhost" /> </conditions> <action type="AbortRequest" /> </rule> <rule name="Capture Origin Header"> <match url=".*" /> <conditions> <add input="{HTTP_ORIGIN}" pattern=".+" /> </conditions> <serverVariables> <set name="CAPTURED_ORIGIN" value="{C:0}" /> </serverVariables> <action type="None" /> </rule> </rules> <outboundRules> <rule name="Set-Access-Control-Allow-Origin for known origins"> <match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern=".+" negate="true" /> <!--<action type="Rewrite" value="{C:0}" /> --> </rule> </outboundRules> </rewrite> <tracing> <traceFailedRequests> <add path="*"> <traceAreas> <add provider="ASP" verbosity="Verbose" /> <add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Verbose" /> <add provider="ISAPI Extension" verbosity="Verbose" /> <add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module,FastCGI,WebSocket" verbosity="Verbose" /> </traceAreas> <failureDefinitions statusCodes="400-599" /> </add> </traceFailedRequests> </tracing> </system.webServer> </configuration> 

对outboundRules我目前有行动type =“重写”行注释掉,因为当我启用它,它会抛出一个错误。

 HTTP Error 500.52 - URL Rewrite Module Error. The page cannot be displayed because an internal server error has occurred. Most likely causes: •IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred. •IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly. •IIS was not able to process configuration for the Web site or application. •The authenticated user does not have permission to use this DLL. •The request is mapped to a managed handler but the .NET Extensibility Feature is not installed. Detailed Error Information: Module: RewriteModule Notification: SendResponse Handler: StaticFile Error Code: 0x80070585 Requested URL: http://localhost:80/iisstart.htm Physical Path: C:\inetpub\wwwroot\iisstart.htm Logon Method: Anonymous Logon User: Anonymous Request Tracing Directory: C:\inetpub\logs\FailedReqLogFiles 

失败的请求日志不是很有帮助,它们显示以下警告:

 411. -MODULE_SET_RESPONSE_ERROR_STATUS ModuleName: RewriteModule Notification: SEND_RESPONSE HttpStatus: 500 HttpReason: URL Rewrite Module Error. HttpSubStatus: 52 ErrorCode: Invalid index. (0x80070585) ConfigExceptionInfo: 

该问题不显示正在发送导致该错误的请求的客户端代码,但是:

客户端代码必须使用XHR或Fetch API (或者使用jQuery或其他库来调用其中的一个库),并且该代码要么将XHR withCredentials属性设置为true要么使用具有该属性的options对象调用Fetch Request构造函数 credentials选项设置为include

在这种情况下,服务器响应具有Access-Control-Allow-Origin: *标头,则浏览器将logging问题中引用的错误。

因此,一个解决scheme是更改JavaScript客户端代码,以便不将XHR withCredentials设置为true并且不使用credentials: 'include'调用Fetch Request构造函数。

另一个解决scheme是让你的服务器端代码取得Origin请求头的值,并将它回显到Access-Control-Allow-Origin响应头的值。

对于IIS,可以通过将以下内容添加到IISconfiguration文件( %SystemDrive%\inetpub\wwwroot\ Web.configApplicationHost.config文件),使用URL重写模块执行此操作。

 <configuration> <system.webServer> <rewrite> <rules> <rule name="Capture Origin Header"> <match url=".*" /> <conditions> <add input="{HTTP_ORIGIN}" pattern=".+" /> </conditions> <serverVariables> <set name="CAPTURED_ORIGIN" value="{C:0}" /> </serverVariables> <action type="None" /> </rule> </rules> <outboundRules> <rule name="Set-Access-Control-Allow-Origin for known origins"> <match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern=".+" negate="true" /> <action type="Rewrite" value="{CAPTURED_ORIGIN}" /> </rule> </outboundRules> </rewrite> </system.webServer> </configuration> 

然后删除任何其他现有的代码/configuration设置Access-Control-Allow-Origin: *

注意:以上是分步指南中示例configuration文件的修改版本使用URL重写为IIS中的特定域启用CORS 。

我有同样的问题,我有一个网站的HTTP和HTTPS绑定与Access-Control-Allow-Origin:*在这种情况下,http请求将失败,出现上述错误。 您可以为http绑定创build一个单独的网站,并从中删除Access-Control-Allow-Origin标题。

只是为了closures这个循环。 上面所提出的答案和build议对于这个问题只能在IIS上使用。

我的问题是IIS-Node库中的一些错误,最终迫使我将整个堆栈迁移到NodeJS并运行Node中本地直接运行的所有模块。