为Azure Rest Webapi应用程序启用CORS [英] Enable CORS for Azure Rest webapi application

查看:54
本文介绍了为Azure Rest Webapi应用程序启用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的jQuery页面,该页面调用Azure宁静的API来获取VM的状态. 我遇到的一个问题是,它抱怨跨源资源共享,而我找不到在哪里为我拥有的Web应用程序/API设置它.

I have simple jQuery page that makes calls to azure restful API to get the status of VMs. I'm facing a problem that it's complaining about Cross-Origin Resource Sharing and I can't find where to set that for the Web app/API I have.

我正在使用客户凭据授予来获取令牌 https://docs.microsoft.com/zh-CN/azure/active-directory/develop/v1-oauth2-client-creds-grant-flow

I'm using client credentials grant to get the token https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-oauth2-client-creds-grant-flow

我已经完成了测试,当我尝试从jQuery/JS进行呼叫时,出现了CORS问题.

I have finished my testing and when I tried to do the calls from jQuery/JS I got the CORS problem.

我的设置涉及: 在Azure门户中,我使用应用程序注册来注册类型为"Web应用程序/API"的应用程序,并为其提供主页地址这是它的住所",并创建了密钥.

My setup involved: From Azure portal, I used App registrations to register an app of type "Web app/API", give it a homepage address "this is where it lives", created a key.

使用

POST https://login.microsoftonline.com/<tenant id>/oauth2/token
grant_type=client_credentials
client_id=application id
client_secret=application key
resource=https://management.core.windows.net/

我错过了什么吗?我的搜索不断将我带到Azure托管的应用程序

Am I missing any missing anything? my search keeps leading me to Azure hosted apps

推荐答案

好的,这是简短的说明:

Okay, here is how to do it in short:

  1. 添加功能应用程序(按请求收费)
  2. 打开新创建的功能应用程序
  3. 在代理"中,从右列表中选择它
  4. 命名,路由模板将是您的新端点URL,后端URL 是您的登录端点,例如: https://login.microsoftonline.com//oauth2/token
  5. 然后,返回到功能应用程序,选择平台功能"选项卡,选择"CORS",将其全部删除,然后输入您的应用程序URL或输入一个简单的*
  1. Add Function App (charge per request)
  2. Open the Newly created function app
  3. In Proxies, select that from the right list
  4. Give it a name, route template will be your new endpoint URL, backend URL is your login endpoint eg: https://login.microsoftonline.com//oauth2/token
  5. After that, back to your function app, select the platform feature tab, Select CORS, delete all of them and enter your application URL or simply a *

您可以更具体地说明这些,但这足以获取令牌.而且所有其他端点都没有CORS问题.

You can be more specific with these, but this is enough to get the token. And all the other endpoint didn't have CORS problem.

祝你好运.

这篇关于为Azure Rest Webapi应用程序启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆