在 Azure 云服务上启用 OPTIONS 方法(以启用 CORS)
Enabling OPTIONS method on Azure Cloud Service (to enable CORS)
我正在使用 Azure API 管理开发 public API,然后调用我的云服务中的服务方法。
要允许其他应用程序在浏览器环境中使用此 API,我需要在云服务中启用 CORS。启用 CORS 涉及处理浏览器作为 pre-flight 发送的 OPTIONS 请求,以检查是否设置了正确的 headers。
为了确保 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 代码,然后我可以在其中设置正确的 headers。
当我从另一个域调用服务时,这在本地一切正常,例如使用 js fiddle。
但是,当我将我的应用程序上传到 Azure 时,它不再有效。任何选项都会立即请求 return 404。
似乎用于将 OPTIONS 请求转发到我的应用程序的处理程序定义在 Azure 上不起作用。
我的问题是:我需要配置什么以确保 OPTIONS 请求到达我的应用程序并且可以在 Azure 上处理?
最后我删除了问题中的处理程序,但添加了两个自定义处理程序:
<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 操作。
按照 Miao Jiang 的建议使用 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>
我在 Azure 上的 Web API 运行 上的 OPTIONS 请求中遇到了类似的问题。有一个简单的修复可用:
- 在 Azure 门户中,单击您的应用服务以打开管理界面。
- 向下滚动管理选项列表,直到到达 'API' 部分,然后单击 'CORS'
- 输入允许来源的网址,或输入“*”以允许全部,然后单击“保存”。
我正在使用 Azure API 管理开发 public API,然后调用我的云服务中的服务方法。 要允许其他应用程序在浏览器环境中使用此 API,我需要在云服务中启用 CORS。启用 CORS 涉及处理浏览器作为 pre-flight 发送的 OPTIONS 请求,以检查是否设置了正确的 headers。
为了确保 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 代码,然后我可以在其中设置正确的 headers。
当我从另一个域调用服务时,这在本地一切正常,例如使用 js fiddle。
但是,当我将我的应用程序上传到 Azure 时,它不再有效。任何选项都会立即请求 return 404。
似乎用于将 OPTIONS 请求转发到我的应用程序的处理程序定义在 Azure 上不起作用。
我的问题是:我需要配置什么以确保 OPTIONS 请求到达我的应用程序并且可以在 Azure 上处理?
最后我删除了问题中的处理程序,但添加了两个自定义处理程序:
<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 操作。 按照 Miao Jiang 的建议使用 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>
我在 Azure 上的 Web API 运行 上的 OPTIONS 请求中遇到了类似的问题。有一个简单的修复可用:
- 在 Azure 门户中,单击您的应用服务以打开管理界面。
- 向下滚动管理选项列表,直到到达 'API' 部分,然后单击 'CORS'
- 输入允许来源的网址,或输入“*”以允许全部,然后单击“保存”。