简体   繁体   English

在Azure Service Bus命名空间上启用CORS

[英]Enable CORS on Azure Service Bus Namespace

I want to use the Azure Service Bus REST API in JavaScript. 我想在JavaScript中使用Azure Service Bus REST API。 Unfortunately I'm running into cross origin problems, as my Service Bus namespace is not in the same domain as my site. 不幸的是,我遇到了跨源问题,因为我的Service Bus名称空间与我的站点不在同一个域中。

I know that you can enable CORS with many Azure services, but I can't find any resource in the Azure documentation or elsewhere telling me how to do this for the Service Bus. 我知道您可以为许多Azure服务启用CORS,但是我无法在Azure文档或其他地方找到任何资源来告诉我如何为服务总线执行此操作。

Does anybody know if, and how, this can be done? 有人知道是否以及如何做到这一点?

I've solved this issue by creating a separate Web App with the following Web.config which is just a simple proxy. 我已经通过使用下面的Web.config创建一个单独的Web应用程序(一个简单的代理)解决了这个问题。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Proxy" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{CACHE_URL}" pattern=".*" />
          </conditions>
          <action type="Rewrite" url="https://mynamespace.servicebus.windows.net/{R:0}" logRewrittenUrl="true" />
      <serverVariables>
        <set name="HTTP_ACCEPT_ENCODING" value="" />
      </serverVariables>
    </rule>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Expose-Headers" value="BrokerProperties" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

Azure Service Bus REST API supports CORS by default. 默认情况下,Azure Service Bus REST API支持CORS。 All origins are accepted. 接受所有来源。

To be honest this is not an Azure specific. 老实说,这不是Azure特定的。 If you are using jQuery you need to enable cross-domain request option, read more here http://api.jquery.com/jQuery.ajax/ . 如果您使用的是jQuery,则需要启用跨域请求选项,请在http://api.jquery.com/jQuery.ajax/中阅读更多信息。 There is also a JavaScript ServiceBus SDK: https://github.com/ddobric/ServiceBusJavaScriptSdk 还有一个JavaScript ServiceBus SDK: https//github.com/ddobric/ServiceBusJavaScriptSdk

Generally from the purity point of view calling Service Bus from client side doesn't look good, I'd rather wrap that call into your own API. 通常,从纯粹的角度来看,从客户端调用Service Bus看起来不太好,我宁愿将该调用包装到您自己的API中。 But I don't know your scenario and it might be valid. 但我不知道您的情况,这可能是有效的。

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

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