Azure API 管理 CORS:为什么我会收到“以‘Access-Control-’开头的标题已被删除……”

csi*_*int 5 azure cors azure-api-management

以下是一个简单的政策:

<policies>
    <inbound>
        <cors>
            <allowed-origins>
                <origin>http://microfost.com/</origin>
            </allowed-origins>
            <allowed-methods preflight-result-max-age="300">
                <method>GET</method>
                <method>POST</method>
                <method>PATCH</method>
                <method>DELETE</method>
            </allowed-methods>
            <allowed-headers>
                <header>content-type</header>
                <header>accept</header>
                <header>Authorization</header>
            </allowed-headers>
        </cors>
    </inbound>
</policies>  
Run Code Online (Sandbox Code Playgroud)

HTTP 请求

OPTIONS https://XXXX.azure-api.net/demo/XXX/XXX/* HTTP/1.1
Host: XXXX.azure-api.net
Ocp-Apim-Trace: true
Ocp-Apim-Subscription-Key: <secret>
Origin: http://microfost.com
Access-Control-Request-Headers: Authorization
Access-Control-Request-Method: GET
Run Code Online (Sandbox Code Playgroud)

回复内容

Access-Control-Allow-Origin: http://microfost.com
Ocp-Apim-Trace-Location: <trace>
Date: Mon, 27 Feb 2017 20:09:14 GMT
Content-Length: 0
Run Code Online (Sandbox Code Playgroud)

我收到此消息并期望 Origin 响应标头 我没有收到 3 个 API 中的 2 个的任何内容(1 个 API 使用与预期相同的策略)。

**Inbound**
[...]
cors (0 ms)
"Cross domain request was well formed and was allowed to proceed. CORS related headers were added to the response."

**Backend**

No records.
Outbound

cors (0 ms)
{
    "message": "Headers starting with 'Access-Control-' were removed from the response. ",
    "headers": []
}
transfer-response (0 ms)
{
    "message": "Response headers have been sent to the caller."
}
Run Code Online (Sandbox Code Playgroud)

在我看来,这是一种无稽之谈,可能是一个错误。在提交之前,我想问你是否有任何解释?为什么我会得到这个?

以“Access-Control-”开头的标题已从响应中删除。

Vit*_*tin 3

在 Azure API 管理中执行 CORS 有两种方法。自动 - 只需在所需范围内删除和配置 CORS 策略,APIM 将负责响应与现有操作匹配的 OPTIONS 请求。

或者您可以选择手动方式 - 创建一个单独的操作来响应 OPTIONS 方法并在策略中手动形成响应,可能使用返回响应策略。

你遇到的问题是因为你两者都有。他们基本上是冲突的。CORS 策略将请求识别为跨源,并安排在请求完成后进行处理,但 OPTIONS 操作级别上的返回响应策略会破坏此处理管道,并在 CORS 策略采取操作之前立即返回响应。

由于您使用的是 CORS 策略,因此您应该从 API 中删除 OPTIONS 操作才能正常工作。