【问题标题】:Azure Function CORS Policy not settings reponse headersAzure Function CORS Policy 未设置响应标头
【发布时间】:2020-06-03 16:42:35
【问题描述】:

我刚刚为我的所有操作设置了 CORS 政策,如下所示:

<policies>
    <inbound>
        <base />
        <cors>
            <allowed-origins>
                <origin>http://localhost:3000</origin>
            </allowed-origins>
            <allowed-methods>
                <method>GET</method>
                <method>POST</method>
            </allowed-methods>
        </cors>
        <validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Failed.">
            <openid-config url="https://*/.well-known/openid-configuration" />
            <audiences>
                <audience>ticket-spa</audience>
            </audiences>
            <issuers>
                <issuer>https://*/</issuer>
            </issuers>
        </validate-jwt>
        <set-header name="X-JWT-SUB" exists-action="override">
            <value>@(context.Request.Headers["Authorization"].First().Split(' ')[1].AsJwt()?.Claims["sub"].FirstOrDefault())</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

但是对 https://*.azure-api.net/myFunctions/items?param=1 的 OPTIONS 请求 结果如下:

HTTP/1.1 200 OK
Request-Context: appId=*
Date: Wed, 03 Jun 2020 07:22:47 GMT
Content-Length: 0

这导致浏览器不允许与我的 API 通信,因为缺少 CORS 标头。

【问题讨论】:

  • 您是否尝试过通过门户或 CLI 设置 CORS 策略? docs.microsoft.com/en-us/azure/azure-functions/… 。我之所以提到这一点,是因为 azure 函数很奇怪,无法从 appsettings 文件正确配置门户中公开的某些设置。

标签: azure cors azure-functions


【解决方案1】:

似乎我必须添加一些允许的标题,如下所示:

<allowed-headers>
                <header>content-type</header>
                <header>accept</header>
                <header>x-tfs-fedauthredirect</header>
                <header>user-agent</header>
                <header>authorization</header>
            </allowed-headers>

仍然奇怪为什么没有错误表明失败的原因。

【讨论】:

    猜你喜欢
    • 2016-04-26
    • 2012-05-01
    • 2018-06-24
    • 2012-06-12
    • 2015-11-25
    • 2019-04-07
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多