首页 文章

在AAD安全Azure Web App中检索访问令牌

提问于
浏览
3

我有Azure Web应用程序中托管的Angular4应用程序和Azure API应用程序中托管的.NET核心Web API .

该API使用Azure Active Directory进行保护 . 目前,我使用ng2-adal获取访问令牌,我将其注入标头以执行我的API调用 .

现在,我尝试使用相同的ClientId(如API)删除ng2-adal模块并使用 Authentication / Authorization 功能保护我的Web App . 当我浏览我的网站时,我被重定向到AAD登录,在我成功登录后,我被重定向到我的网站 . 现在我想找到一种方法来检索令牌 .

在这种情况下,有没有办法让 retrieve the access token within my Angular App ?看起来令牌在AppServiceAuthSession Cookie中加密存储:

enter image description here

2 回答

  • 0

    AppServiceAuthSession 是与 token 不同的cookie . 在这种情况下,您需要修改Azure应用程序的配置,以使其获取Web API的access_token .

    我们可以使用Resource Explore修改如下设置:

    1 . 找到有角度的网络应用程序

    2 . 找到config-> authsettings(资源是Azure应用程序的clientId,用于保护您的应用程序)

    "additionalLoginParams": [
      "response_type=code id_token",
      "resource=3fa9607b-63cc-4050-82b7-91e44ff1df38"
    ],
    

    3.为Azure应用程序配置redirect_uri,如下所示: https://appfei.azurewebsites.net/.auth/login/aad/callback

    然后在登录角度应用程序后,您可以通过 endpoints 获取 access_tokenhttps://appfei.azurewebsites.net/.auth/me

    enter image description here

    然后我们需要使用下面的 Advanced Azure Active Settings 来保护Web API,以使access_token可以调用Web API:
    enter image description here

  • 4

    我已经为此工作了一个星期 . 所以,我想分享一下我是如何得到它的 .

    我能够使用AAD对我的应用进行身份验证 .

    我的cookie存储中有AppServiceAuthSession .

    然后在我的应用程序中,我调用了auth / me API .

    https://yourwebsite.azurewebsites.net/.auth/me

    所以,它就像:

    this.$http
      .get('https://yourwebsite.azurewebsites.net/.auth/me').then(response => {
        console.log(".auth/me", response)
      }, err => {
        console.log("Error: ", err)
      })
    

    我其实是在使用Vue . 调用您的HTTP可能会有所不同 . 就是这样 .

    我基本上调用了auth / me API来检索我需要的信息 .

    附:您当然需要进行身份验证 .

相关问题