IIS中的CORS问题与Access-Control-Allow-Origin中的凭据和通配符有关

时间:2017-02-03 19:44:04

标签: node.js iis cors credentials

我继承了一个相当基本的站点,它提供数据并处理一些套接字连接。它使用iisnode作为桥接器在IIS后面运行NodeJS。所有这些都可以在#34;正常页面上正常工作"透视图。

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

我已将Access-Control-Allow origin设置为*,只是将谷仓门打开但现在在客户端出现以下错误:

  

11:29:57.668阻止跨源请求:同源策略   禁止阅读远程资源   “http://server/socket.io/?EIO=3&transport=polling&t=1486150196479-0”。   (原因:如果CORS标头不支持Credential   'Access-Control-Allow-Origin'是'*')。 1(未知)

我试图将Access-Control-Allow-Credentials明确地设置为false(以及true,以及完全保留)但是我的尝试都没有让我超越它。

原始响应标头目前看起来像这样:

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的评论进行了一些挖掘工作,这些评论让我想到了这个article还有一些额外的步骤来添加变量,以及其他一些东西来获得这个变量工作

我的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上的

我目前有动作类型=&#34;重写&#34; line注释掉了,因为当我启用它时,它会引发错误。

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: 

3 个答案:

答案 0 :(得分:3)

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

客户端代码必须使用XHR或Fetch API(或使用jQuery或其他调用其中一个的库),并且该代码要么将XHR withCredentials属性设置为{{1}或者正在使用true选项设置为credentials的选项对象调用抓取Request constructor

如果是这种情况并且服务器响应具有include标题,则您的浏览器将记录问题中引用的错误。

因此,一个解决方案是更改JavaScript客户端代码,因此它不会将XHR Access-Control-Allow-Origin: *设置为withCredentials而不会使用true调用获取请求构造函数。

另一种解决方案是让服务器端代码获取credentials: 'include'请求标头值,并将其回显到Origin响应标头值。

对于IIS,您可以使用URL Rewrite Module执行此操作,方法是将以下内容添加到IIS配置文件(Access-Control-Allow-Origin中的Web.configApplicationHost.config)。

%SystemDrive%\inetpub\wwwroot\

然后删除其他现有代码/配置设置<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>

注意:以上是分步指南Enable CORS for specific domains in IIS using URL Rewrite中示例配置文件的修改版本。

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

关闭此循环。上面发布的答案和建议适用于仅限IIS的网站。

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