【问题标题】:Enabling OPTIONS method on Azure Cloud Service (to enable CORS)在 Azure 云服务上启用 OPTIONS 方法(启用 CORS)
【发布时间】:2018-10-29 15:31:18
【问题描述】:

我正在使用 Azure API 管理开发一个公共 API,然后调用我的云服务中的服务方法。 为了允许其他应用程序在浏览器环境中使用此 API,我需要在云服务中启用 CORS。启用 CORS 涉及处理浏览器发送的 OPTIONS 请求,作为预检,以检查是否设置了正确的标头。

为了确保 OPTIONS 请求到达我的应用程序,我不得不在我的 web.config 中进行一些更改:

<system.webServer>
  <handlers>
    <remove name="SimpleHandlerFactory-Integrated-4.0" />
    <remove name="SimpleHandlerFactory-Integrated" />
    <remove name="SimpleHandlerFactory-ISAPI-4.0_64bit" />
    <remove name="SimpleHandlerFactory-ISAPI-4.0_32bit" />
    <remove name="SimpleHandlerFactory-ISAPI-2.0-64" />
    <remove name="SimpleHandlerFactory-ISAPI-2.0" />
    <remove name="OPTIONSVerbHandler" />
    <add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="bitness32" />
    <add name="SimpleHandlerFactory-ISAPI-2.0" path="*.ashx" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" responseBufferLimit="0" />
    <add name="SimpleHandlerFactory-ISAPI-2.0-64" path="*.ashx" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness64" responseBufferLimit="0" />
    <add name="SimpleHandlerFactory-ISAPI-4.0_32bit" path="*.ashx" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
    <add name="SimpleHandlerFactory-ISAPI-4.0_64bit" path="*.ashx" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
    <add name="SimpleHandlerFactory-Integrated" path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv2.0" />
    <add name="SimpleHandlerFactory-Integrated-4.0" path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
  . . .
</system.webServer>

这可确保 OPTIONS 调用到达我的 .ashx 和 .svc 代码,然后我可以在其中设置正确的标头。

当我从另一个域调用服务时,这一切都可以在本地正常工作,例如使用 js fiddle。

但是,当我将应用程序上传到 Azure 时,它​​不再工作。任何 OPTIONS 请求都会立即返回 404。

似乎用于将 OPTIONS 请求转发到我的应用程序的处理程序定义在 Azure 上不起作用。

我的问题是:我需要配置什么来确保 OPTIONS 请求到达我的应用程序并可以在 Azure 上处理?

【问题讨论】:

标签: azure cors azure-api-management


【解决方案1】:

最后,我最终删除了问题中的处理程序,但添加了两个自定义处理程序:

<remove name="SimpleHandlerFactory-Integrated-4.0" />
<remove name="SimpleHandlerFactory-Integrated" />
<remove name="SimpleHandlerFactory-ISAPI-4.0_64bit" />
<remove name="SimpleHandlerFactory-ISAPI-4.0_32bit" />
<remove name="SimpleHandlerFactory-ISAPI-2.0-64" />
<remove name="SimpleHandlerFactory-ISAPI-2.0" />
<remove name="OPTIONSVerbHandler" />
<!-- Added the following handlers -->
<add name="AshxHandler" path="*.ashx" verb="*"
    type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified"
    requireAccess="Script"/>
<add name="SvcHandler" path="*.svc" verb="*"
    type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified"
    requireAccess="Script"/>

答案的另一部分是在 API 管理中添加 OPTIONS 操作。 使用 Miaojiang 所建议的 API 管理的 CORS 策略实际上并没有奏效,而且当我现在包含它时,实际上在我的情况下破坏了 CORS。 不过,我确信它适用于其他情况。

编辑:我最终还是使用了策略,现在它可以工作了。不需要添加任何 OPTIONS 操作。我在 API 级别使用了以下策略:

<policies>
    <inbound>
        <base />
        <cors>
            <allowed-origins>
                <origin>*</origin>
            </allowed-origins>
            <allowed-methods>
                <method>GET</method>
                <method>POST</method>
                <method>OPTIONS</method>
            </allowed-methods>
            <allowed-headers>
                <header>*</header>
            </allowed-headers>
        </cors>
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>

【讨论】:

    【解决方案2】:

    我在 Azure 上运行的 Web API 上遇到了类似的 OPTIONS 请求问题。有一个简单的修复方法:

    1. 在 Azure 门户中,单击您的应用服务以打开管理界面。
    2. 向下滚动管理选项列表,直到到达“API”部分,然后点击“CORS”
    3. 要么输入允许来源的网址,要么输入“*”以允许全部,然后单击保存。

    【讨论】:

    • 问题是针对 Azure 云服务,与 Azure 应用服务无关
    猜你喜欢
    • 2014-07-30
    • 2018-07-07
    • 2014-04-16
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    相关资源
    最近更新 更多