azure - 从静态文件 SPA 调用 Azure Function App

标签 azure azure-functions azure-web-app-service easy-auth

解答:对于寻找此答案的任何人,我可以通过在 MSAL acquireToken 调用范围内指定 Web API 来获取所需的 token ,如下所示:

let token = msal.acquireToken({ 
  scopes: [ 'myFunctionApp.azurewebsites.net/user_impersonation' ] 
})

完成此操作后,我将该 token 用作身份验证 header 中的承载 token 。除了调用 MS Graph 端点之外,我还可以使用它。我在一个安静的偏僻地方找到了此信息:

https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-acquire-cache-tokens#request-specific-scopes-for-a-web-api

非常感谢@StanleyGong 的帮助,最终找到了答案。

/////

过去几天我一直在寻找如何有效地保护从调用 Azure Function App 的 Azure Web 应用程序提供的 Vue SPA。我为 Web 应用程序和函数应用程序打开了 Easy Auth,并且我正在调用 /.auth/me 端点来获取 id token ,我读过该 token 可以用作不记名 token 。所以我正在做的是调用 /.auth/me 并使用返回的 id_token 创建授权 header ,但我仍然得到 401 Unauthorized 调用函数 app 时。

从/.auth/me 获取 id_token 并将其作为默认 header 添加到所有 Axios 调用的代码(我确实认识到这需要刷新...我将在获得后创建所有逻辑一次调用即可工作):

let authUrl = '/.auth/me';
let response = await axios.get(authUrl);
let token = response.data[0].id_token;
axios.defaults.headers.common['Authorization'] = "Bearer " + token;

我可以看到请求中的授权 header 中使用了 token ,该 header 紧接在上面的部分之后:

Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Bearer eyJ0eXAiO...

我看到了这篇文章并尝试使用其中的一些信息,包括 Chris Gillum 的博客文章(这很有帮助,但仍然没有让我到达那里):Authentication for Azure Functions

关于如何让它发挥作用有什么建议吗?我觉得我已经很接近了,但我还没有完全做到这一点。如果这不是正确的方法,任何建议也会有所帮助。

总的来说,我在身份方面缺乏经验,所以我一直在使用术语,而文档中的不一致并没有帮助我。

此外,有没有什么方法可以测试这些东西,而无需在每次更改时部署代码?通过以某种方式从商店获取 token 而无需将代码放在网络应用程序服务器上来测试这一点会很棒......我猜这是一厢情愿的想法,但只是想知道。

编辑:我刚刚意识到我读过的所有帖子都表明访问 token 是从 /.auth/me 返回的,但我没有得到。返回的 JSON 看起来像这样,这就是全部:

id_token: "eyJ0eXA...7_A"
provider_name: "aad"
user_claims: [{typ: "aud", val: "2...fa"},…]
user_id: "<<my email address>>"

另一个编辑:我发现我可以使用 MSAL 获取 token ,但它包含的信息与 /.auth/me 中的信息略有不同。使用任一 token 作为不记名 token 仍然会导致 401。我确实注意到,在应用程序的 AAD 身份验证设置中,颁发者 URL 不同。一个是 sts.windows.net,另一个是 login.windows.net。两人身上都贴有租户 ID。不确定这是否会产生影响,但我尝试将它们设置为相同的值,但这没有帮助。

/.auth/me token (当然已清理):

{
  "aud": "2fe...fa", (AAD app id)
  "iss": "https://sts.windows.net/<< AD tenant id >>/",
  "iat": 15785xxx,
  "nbf": 15785xxx,
  "exp": 15785xxx,
  "aio": "AVQAq/...UQ=",
  "amr": [
    "pwd",
    "mfa"
  ],
  "family_name": "<< my last name >>",
  "given_name": "<< my first name >>",
  "ipaddr": "<< my ip >>",
  "name": "<< my full name >>",
  "nonce": "e32a...48",
  "oid": "a0...0e",
  "sub": "LNu...8l8",
  "tid": "f14...2ca",
  "unique_name": "<< my email >>",
  "upn": "<< my email >>",
  "uti": "i9O...TAQ",
  "ver": "1.0"
}

MSAL 访问 token :

{
  "aud": "000...000", (mostly 0s...not sure what this id is)
  "iss": "https://sts.windows.net/<< AD tenant id >>",
  "iat": 15785xxx,
  "nbf": 15785xxx,
  "exp": 15785xxx,
  "acct": 0,
  "acr": "1",
  "aio": "AVQAq/8O...ZZ12s=", (different than above)
  "amr": [
    "pwd",
    "mfa"
  ],
  "app_displayname": "<< app name in AAD app registration >>",
  "appid": "<< app GUID from AAD >>",
  "appidacr": "0",
  "family_name": "<< my last name >>",
  "given_name": "<< my first name >>",
  "ipaddr": "<< my ip >>",
  "name": "<< my full name >>",
  "oid": "a0...0e", (same as above)
  "platf": "3",
  "puid": "10...1B",
  "scp": "User.Read profile openid email",
  "signin_state": [
    "kmsi"
  ],
  "sub": "WuX...L3A",
  "tid": "f14...2ca", (tenant id, same as above)
  "unique_name": "<< my email >>",
  "upn": "<< my email >>",
  "uti": "UD...AA",
  "ver": "1.0",
  "xms_st": {
    "sub": "LNu...8l8"
  },
  "xms_tcdt": 14...37
}

最佳答案

对于您的方案,您可以在租户中注册 Azure AD native 应用程序作为客户端,以获取 access_token 作为承载 token 来调用您的 Azure 函数。如果您使用服务到服务调用,只需引用my previous post here

如果您要登录用户,您还应该使用其他配置在您的租户中注册 Azure AD 应用程序: enter image description here 向此应用程序添加 user_impersonation 权限,以便用户可以登录您的应用程序并调用您的 Azure 函数(请在此处记下您的 azure 函数应用程序 ID,我们稍后将使用它): enter image description here 添加此权限并授予它: enter image description here enter image description here

完成这些步骤后,您的新应用程序将能够登录用户来调用您的 Azure 函数。

我不确定你的VUE应用程序代码是什么样的,但这是一个VUE adal sample我的演示将基于它。 1.下载代码并前往src/main.js将其内容替换为以下代码:

import Vue from 'vue'
import axios from 'axios'
import { default as Adal, AxiosAuthHttp } from 'vue-adal'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = false
const functionBase = `<your Azure function URL>`
const functionResource = '<your Azure function application ID>'

Vue.use(Adal, {
  config: {
    tenant: '<your tenant ID>',
    clientId: '<your new resistered app ID>',
    redirectUri: '<redirect url you mapped in your new resgistered app ID, in this case it should be : http://localhost:8080>',
    cacheLocation: 'localStorage'
  },
  requireAuthOnInitialize: true,
  router: router
})

Vue.use({
  install (vue, opts = {}) {
    // Configures an axios http client with a interceptor to auto-acquire tokens
    vue.prototype.$functionApi = AxiosAuthHttp.createNewClient({
      // Required Params
      axios: axios,
      resourceId: functionResource, // Resource id to get a token against

      // Optional Params
      router: router, // Enables a router hook to auto-acquire a token for the specific resource

      baseUrl: functionBase, // Base url to configure the client with

      onTokenSuccess (http, context, token) { // Token success hook
        // When an attempt to retrieve a token is successful, this will get called.
        // This enables modification of the client after a successful call.
        if (context.user) {
          // Setup the client to talk with the Microsoft Graph API
          http.defaults.baseURL = `${functionBase}`
          console.log(token)
        }
      },

      onTokenFailure (error) { // Token failure hook
        // When an attempt to retrieve a token is not successful, this will get called.
        console.log(error)
      }
    })
  }
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

2. 前往src/views/Home.vue ,替换<script>的内容代码部分如下:

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'home',
  components: {
    HelloWorld
  },
  data () {
    return {
      msg: "Signing in..."
    }
  },
  async created () {
    if (this.$adal.isAuthenticated()) {
      this.msg = "Hello, " + this.$adal.user.profile.name 

      let functionInfo = await this.getAzureFunctionInfo()
      this.msg += "  |  function result : " + functionInfo


    } else {
      this.msg = "Please sign in"
    }
  },

  methods: {
    async getAzureFunctionInfo () {
      let res = await this.$functionApi.get('',{
        params: {
          'name': 'test'
        }
      })
      console.log(res)
      return res.data
    }
  }
}
</script>

让我们测试一下结果: 由于此项目将在本地运行,您应该转到 Azure 函数 => 平台功能 => 所有设置以启用所有 cors 请求,以便您可以从静态 vue 项目在本地测试您的函数: enter image description here

我的函数逻辑很简单,如果你用参数调用它,它会回复一个 hello 响应: enter image description here

运行项目,并通过 url 在私有(private)浏览器窗口中打开它: http://localhost:8080 您将被要求登录,您可以看到它可以成功调用Azure函数: enter image description here

我认为这就是您正在寻找的答案。

更新:

根据您的代码,您正在使用 Azure AD V2 登录用户,请:

1)按照第二种解决方案重新注册Azure广告应用并完成权限授予过程,以便它可以登录用户并访问您的Azure功能。

2) 在您的 Azure AD 中,找到代表您的 Azure 函数应用程序名称的 Azure AD 应用程序(我的 Azure 函数名称是 :stanfuntest ): 将范围复制到此处: enter image description here

2)在您的VUE代码中,使用此应用程序ID作为客户端ID来登录用户,修改代码以获取您刚刚找到的范围的访问 token :

let token = msal.acquireToken( { scopes: [ 'https://stanfuntest.azurewebsites.net/user_impersonation' ] } )

希望有帮助。

关于azure - 从静态文件 SPA 调用 Azure Function App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59637635/

相关文章:

Azure ML 数据集版本控制 : What is Different if it Points to the Same Data?

c# - Azure Cosmos/Document DB ResourceResponse<Document> 使用反射错误进行单元测试

c# - 在 Visual Studio 中使用 C# 脚本开发 Azure Function

c++ - 带有 c++ dll 的 azure webapp 无法正常工作

c# - 添加数据库迁移代码时 Azure 应用服务崩溃

azure - 有没有办法像使用 "AzureAD"模块一样在 "Az"powershell 模块中的身份验证上下文之间进行切换?

azure - 如何使用 bicep 在多个服务总线中创建多个主题/队列?

c# - C# 中的 Azure Function App 是否可以执行命令行应用程序?

python - 如何添加 Blob 存储绑定(bind)?

azure - 如何使用 Azure 网站预览设置自定义 DNS?