【问题标题】:CORS in IIS issue with credentials and wildcard in Access-Control-Allow-OriginIIS 中的 CORS 与 Access-Control-Allow-Origin 中的凭据和通配符问题
【发布时间】:2017-06-21 06:37:01
【问题描述】:

我继承了一个相当基本的站点,它提供数据并处理一些套接字连接。它使用 iisnode 作为桥梁在 IIS 后面运行 NodeJS。从“提供普通页面”的角度来看,这一切都很好。

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

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

11:29:57.668 跨域请求被阻止:同源策略 不允许读取远程资源 ‘http://server/socket.io/?EIO=3&transport=polling&t=1486150196479-0’。 (原因:如果 CORS 标头不支持凭据 “访问控制允许来源”是“*”)。 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 的 cmets 进行更多挖掘,这使我得到了这个 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 上,我目前已将 action type="Rewrite" 行注释掉,因为当我启用它时,它会引发错误。

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: 

【问题讨论】:

  • 如果问题还包括客户端 JavaScript 代码会很有用
  • 请尝试&lt;action type="Rewrite" value="{CAPTURED_ORIGIN}" /&gt; 或者甚至&lt;action type="Rewrite" value="{HTTP_ORIGIN}" /&gt;(如果value="{HTTP_ORIGIN}" 在那里工作,那么整个&lt;rule name="Capture Origin Header"&gt; 元素可以被删除)。
  • 啊,第一个明白了,谢谢!我没有意识到这些替换是在本地范围内的。非常感谢!
  • 超级——很高兴它终于为你工作了。 (我更新了我的答案以使用&lt;action type="Rewrite" value="{CAPTURED_ORIGIN}" /&gt;

标签: node.js iis cors credentials


【解决方案1】:

只是为了结束这个循环。关于这个问题,上面发布的答案和建议对于仅 IIS 的网站来说效果很好。

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

【讨论】:

  • 这不是问题的实际答案。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    如果是这种情况并且服务器响应具有 Access-Control-Allow-Origin: * 标头,您的浏览器将记录问题中引用的错误。

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

    另一种解决方案是让您的服务器端代码采用Origin request-header 值并将其回显到Access-Control-Allow-Origin response-header 值。

    对于 IIS,您可以通过将以下内容添加到 IIS 配置文件(%SystemDrive%\inetpub\wwwroot\ 中的Web.configApplicationHost.config 文件)来使用 URL Rewrite Module

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

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

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

    【讨论】:

    • 谢谢,您的帖子看起来至少部分引用了这篇文章,除非您没有使用 Rewrite Map:[link]carlosag.net/articles/… 我将更新我的配置部分已实现,以及客户端代码 - 这是我正在尝试连接的 socket.io 连接。
    • 是的,我遗漏的部分不是必需的,除非您还想限制对来自特定域的请求的访问。 (并且我更新了我的答案以归功于配置文件的来源。)
    • 是的,我首先尝试实现您建议的条目,这看起来正是我需要为“所有域”做的事情 - 但我仍然在这一行遇到 500.52 错误:&lt;action type="Rewrite" value="{C:0}" /&gt;
    猜你喜欢
    • 2021-05-12
    • 2016-09-20
    • 2016-09-30
    • 2017-10-24
    • 2017-11-21
    • 2019-06-17
    • 2014-02-18
    • 2014-07-08
    相关资源
    最近更新 更多