简体   繁体   English

在Azure云服务上启用OPTIONS方法(以启用CORS)

[英]Enabling OPTIONS method on Azure Cloud Service (to enable CORS)

I am developing a public API using Azure API Management, which then calls service methods in my cloud service. 我正在使用Azure API管理开发公共API,然后在我的云服务中调用服务方法。 To allow other apps to use this API from a browser environment, I need to enable CORS in the cloud service. 为了允许其他应用程序在浏览器环境中使用此API,我需要在云服务中启用CORS。 Enabling CORS involves handling OPTIONS requests which are sent by the browser as a pre-flight to check if the correct headers are set. 启用CORS涉及处理浏览器发送的OPTIONS请求,以检查是否设置了正确的标头。

To make sure the OPTIONS request reaches my application I have had to make a few changes in my web.config: 为了确保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>

This ensures that the OPTIONS calls reach my .ashx and .svc code in which I can then set the correct headers. 这样可以确保OPTIONS调用到达我的.ashx和.svc代码,然后可以在其中设置正确的标头。

This all works fine locally when I call the services from another domain, for example using js fiddle. 当我从另一个域调用服务时,例如使用js小提琴,这些在本地都可以正常工作。

However, when I upload my application to Azure, it no longer works. 但是,当我将应用程序上载到Azure时,它不再起作用。 Any OPTIONS requests return a 404 immediately. 任何OPTIONS请求都将立即返回404。

It seems that the handler definitions used to forward the OPTIONS requests to my applications do not work on Azure. 似乎用于将OPTIONS请求转发到我的应用程序的处理程序定义在Azure上不起作用。

My question is: what do I need to configure to make sure OPTIONS requests reach my application and can be handled there on Azure? 我的问题是:我需要配置什么以确保OPTIONS请求到达我的应用程序并可以在Azure上处理?

In the end I ended up removing the handlers as in my question, but adding two custom 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" />
<!-- 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"/>

Another part of the answer was adding the OPTIONS operations in API Management. 答案的另一部分是在API管理中添加OPTIONS操作。 Using the CORS policy of API Management as suggested by Miao Jiang actually did not work and in fact breaks CORS in my situation when I include it now. 苗江建议使用API​​管理的CORS策略实际上是行不通的,实际上,当我现在将其包括在内时,我的情况会中断CORS。 I'm sure it will work for other situations though. 我敢肯定,它将在其他情况下使用。

EDIT: I ended up using policies anyway, and it now works. 编辑:无论如何,我最终还是使用了策略,现在可以了。 No OPTIONS operations need to be added. 无需添加OPTIONS操作。 I've used the following policy on API level: 我在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>

I had a similar issue with OPTIONS request on an Web API running on Azure. 我在Azure上运行的Web API上的OPTIONS请求有类似的问题。 There was a simple fix available: 有一个简单的修复程序:

  1. In the Azure Portal, click on your App Service to open the management interface. 在Azure门户中,单击您的应用程序服务以打开管理界面。
  2. Scroll down the list of management options until you reach the 'API' section, and click on 'CORS' 向下滚动管理选项列表,直到找到“ API”部分,然后单击“ CORS”
  3. Either enter the web address of the allowed origin, or enter '*' to allow all, and click Save. 输入允许的来源的网址,或输入“ *”以允许所有来源,然后单击“保存”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM