简体   繁体   English

有没有办法在 Azure 虚拟机上启用 CORS?

[英]Is there a way to enable CORS on an Azure Virtual Machine?

Is there a way to enable CORS on an Azure Virtual Machine ?有没有办法在 Azure 虚拟机上启用 CORS? I am running a flask at localhost:5001 and angular app at localhost:4200 In my local environment, it woks perfectly.我在 localhost:5001 上运行一个烧瓶,在 localhost:4200 上运行 angular 应用程序在我的本地环境中,它完美地工作。 However, in the virtual machine instance on azure it doesn't work because I can't setup my CORS.但是,在 azure 上的虚拟机实例中它不起作用,因为我无法设置我的 CORS。

To enable CORS in azure , we need to configure a policy in APIs by following below steps.要在 azure 中启用 CORS,我们需要按照以下步骤在 API 中配置策略。

1. Add the APIs required in the azure portal . 1. 添加azure 门户中所需的API

在此处输入图片说明

  • Apply the below changes in your deployed application/applications in iis if not done alread in Web.config file.如果 Web.config 文件中尚未完成,请在 iis 中部署的应用程序/应用程序中应用以下更改。

 <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> </customHeaders> </httpProtocol> </system.webServer>

To see how to go to web.config file you may refer this Link to Configure CORS in azure要查看如何转到 web.config 文件,您可以参考此链接以在 azure 中配置 CORS

2. Go to APIs >> click on all operations >> Add policy 2. 前往 APIs >> 单击所有操作 >> 添加策略

在此处输入图片说明

- There you can see cors.Click on it. - 在那里你可以看到 cors。点击它。

在此处输入图片说明

- Then click on save - 然后点击保存

在此处输入图片说明

- You will need to navigate to the inbound policy and check if you have this element added. - 您需要导航到入站策略并检查是否添加了此元素。 在此处输入图片说明

  • You can edit in form-editor directly or code-editor as below:您可以直接在表单编辑器或代码编辑器中进行编辑,如下所示:

在此处输入图片说明

  • This example demonstrates how to support pre-flight requests, such as those with custom headers or methods.此示例演示如何支持飞行前请求,例如具有自定义标头或方法的请求。 To support custom headers and支持自定义标题和
    additional HTTP verbs, use the allowed-methods and allowed-headers额外的 HTTP 动词,使用 allowed-methods 和 allowed-headers
    sections as shown in the following example.部分,如以下示例所示。 - If your service supports CORs for any domain , you place “*” as shown below - 如果您的服务支持任何域的 COR,则如下所示放置“*”

在此处输入图片说明

(Or) (要么)

  • If you want cors only few specific URIs to call your service you may add them in origin tag or you may directly fill the form by using form editor.You may add the methods that require for your service.如果您只希望 cors 只有几个特定的​​ URI 来调用您的服务,您可以将它们添加到 origin 标签中,或者您可以使用表单编辑器直接填写表单。您可以添加您的服务所需的方法。

在此处输入图片说明

You may refer this Document for more information .您可以参考本文档了解更多信息。


You may Enable the Access-Control-Allow-Credentials header.您可以启用 Access-Control-Allow-Credentials 标头。 This is necessary whenever you need to send cookies or a token as part of calling your API >> Reference每当您需要发送 cookie 或令牌作为调用 API 的一部分时,这是必要的 >> 参考


(OR) (要么)

  • In the Cloud Shell, enable CORS to your client's URL by using the az webapp cors add command.在 Cloud Shell 中,使用az webapp cors add命令为客户端的 URL 启用 CORS。 Replace the placeholder.替换占位符。

In AZURE CLI:在 AZURE CLI 中:

webapp cors add --resource-group myResourceGroup --name <app-name> --allowed-origins 'http://localhost:5000'
  • You can set more than one client URL in properties.cors.allowedOrigins ("['URL1','URL2',...]").您可以在 properties.cors.allowedOrigins ("['URL1','URL2',...]") 中设置多个客户端 URL。 You can also enable all client URLs with "['*']".您还可以使用“['*']”启用所有客户端 URL。 You can refer this for more info regarding this.您可以参考了解有关此内容的更多信息。

Other references:其他参考:

  1. Troubleshoot CORS error 排查 CORS 错误
  2. app-service-api-cors-consume app-service-api-cors-consume
  3. Code examples-CORS 代码示例-CORS
  4. App-service-web-tutorial-rest api App-service-web-tutorial-rest api
  5. CORS proxy CORS 代理
  6. Azure CDN with CORS带有 CORS 的 Azure CDN

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

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