【问题标题】:Authenticate against an Azure Mobile Service App with ADAL.js acquired token使用 ADAL.js 获取的令牌针对 Azure 移动服务应用进行身份验证
【发布时间】:2015-08-18 23:08:32
【问题描述】:

我正在尝试针对 Azure 移动服务应用对 HTML 应用进行身份验证。

设置

两个应用程序都使用 AAD 作为身份验证后端,因此两个应用程序都在 Active Directory 中注册了一个应用程序:

Azure 移动服务应用:

HTML 应用程序:

  • 在“对其他应用程序的权限”中,我添加了具有委派权限“访问”的 Azure 移动服务应用程序

Azure 移动服务使用 .NET 后端,我在其中包含并配置了 NuGet 包“Microsoft Azure 移动服务 .NET 后端安全扩展”,如 https://azure.microsoft.com/en-gb/documentation/articles/mobile-services-dotnet-backend-windows-phone-get-started-users/ 中所述

HTML 应用使用 ADAL.JS 和 Angular:

adalAuthenticationServiceProvider.init(
{
    // Config to specify endpoints and similar for your app
    clientId: "<html app aad client id>",
    redirectUri: "<html app redirect uri>",
    endpoints: {
        '<AMS app client id>': 'https://ampapp.azure-mobile.net/'
    }
},
$httpProvider
);

此设置按预期工作,我打开我的 html 应用程序,针对 Azure AD 进行身份验证,重定向到我的应用程序并登录。此外,当我尝试访问我的 Azure 移动服务时,我看到了 Adal.js注入不记名令牌。

问题

Azure 移动服务不接受不记名令牌 - 我收到 401 未授权。我不知道为什么,但 Azure 移动服务使用它自己的身份验证标头 - 但没关系。

MSDN 为 Azure 移动服务定义了所谓的“客户端登录操作”:

“使用已从身份提供者获得的身份令牌从 Microsoft Azure 移动服务请求身份验证令牌。” (https://msdn.microsoft.com/en-us/library/azure/jj710106.aspx)

好的,让我们这样做:

 // obtain token for Azure Mobile Service from Adal.js
 var token = this.getAADToken(ZUMOAuthenticationProvider.Config().url);

 $http({
        method: 'POST',
        url: ZUMOAuthenticationProvider.Config().url + 'login/aad', 
        data: JSON.stringify({
                  "access_token" : token 
              }),
        headers: {
                 'X-ZUMO-APPLICATION': '<application key>'
       }).
       success(function (data, status, headers, config) {
            alert(data);
       }).
       error(function (data, status, headers, config) {
            alert(data);
       }); 

注意:第一行获取的令牌实际上是 azure 移动服务 aad 应用程序的访问令牌,而不是 HTML 应用程序的访问令牌。

这个 POST 请求也会得到 401 响应。所以我不知道如何验证我的应用程序。我还尝试了 azure 移动服务 js lib。这个库可以工作,但它使用弹出窗口进行身份验证,但我不喜欢为我的项目添加另一个库,只是为了几个 REST 调用。

类似问题

在尝试解决我的问题时,我发现了其他 Stackoverflow 帖子:

Why isn't my Azure Mobile Service accepting the bearer token ADAL.js is sending it?

  • 同样的问题,没有解决方案(即使在最后一条评论中链接的聊天日志中)

How do I secure an Azure Mobile Service with Azure AD? ADAL.JS

  • 与上述相同的作者,我检查了接受的答案中提到的所有内容,但它不起作用

我还从新的 Azure 管理门户查看了新的 Azure 移动应用,但它们似乎使用相同的身份验证机制。

那么,我怎样才能让它工作呢?

【问题讨论】:

  • 那么,您在 AAD 中设置了两个应用程序吗?您是否在它们上都启用了客户端流?
  • 是的,Azure AD 中有 2 个应用程序,两个客户端流都已启用,因为它们都应该能够通过 JS 进行身份验证。重要提示:应授予 HTML 应用访问 Azure 移动服务
  • 您能否将您发送到 /login/aad 端点的 JWT 示例发送给我,以便我可以将其与我的进行比较。
  • 检查 JWT 中的有效负载的好资源是“jwt.io”这个网站有工具可以解码你的令牌内容。我在验证使用 Auth0 获得的令牌时遇到问题。我将此与使用纯天蓝色时获得的令牌进行了比较,发现它完全不同。

标签: c# azure mobile azure-mobile-services adal


【解决方案1】:

好的,我发现了我的错误:

endpoints: {
    '<AMS app client id>': 'https://ampapp.azure-mobile.net/'
}

这应该是

endpoints: {
    'https://ampapp.azure-mobile.net/': '<AMS app id uri>': 
}

在此之后它工作!我打算向 github 发布一个 Angular 模块,它将 X-Auth-User 标头中的令牌注入到每个请求中,就像 adal.js 一样。

编辑:

这里承诺一个更详细的答案:

正如我的问题中提到的,您必须在 Azure Active Directory 中设置 2 个应用程序:

  • Azure 移动服务的 AAD 应用程序
    • 只需按照article 中的说明进行操作即可
  • HTML 应用程序的 AAD 应用程序
    • 将“oauth2AllowImplicitFlow”设置为“true”
    • 在“其他应用程序的权限”下添加 Azure 移动服务 AAD 应用程序

将 Angular 应用配置为使用 Azure 移动服务作为端点

adalAuthenticationServiceProvider.init(
{
    clientId:"54110492-4ae3-4c9f-9530-3101458d43fb",
    redirectUri: "https://localhost:44304/",
    endpoints: {
        'https://zumodemoapp.azure-mobile.net/': 'https://zumodemoapp.azure-mobile.net/login/aad'
    }
},
$httpProvider
);

现在您可以使用Client-directed login operation 获取 Azure 移动服务身份验证令牌。

var zumoAppID = 'https://zumodemoapp.azure-mobile.net/login/aad';
var zumoLoginUri = 'https://zumodemoapp.azure-mobile.net/login/aad';
var zumoTodoController = 'https://zumodemoapp.azure-mobile.net/tables/TodoItem';

// 1. acquire a oath token for our zumo app from azure ad via adal.js
adalAuthenticationService.acquireToken(zumoAppID).then(function (data) {
     //2. we have the azure ad token, lets get a azure mobile service token
     $http.post(zumoLoginUri,
                JSON.stringify({
                    "access_token": data
                })).
                success(function (data, status, headers, config) {
                    //3. with the azure mobile service token we can authenticate our request
                    $http.get(zumoTodoController,
                                          {
                                              headers:  {
                                                      'X-ZUMO-AUTH': data.authenticationToken
                                              }
                                          }).
                                          success(function (data, status, headers, config) {
                                              alert(data); //yay!
                                          });
                }).
                error(function (data, status, headers, config) {
                    alert(data);
                });
});

正如评论中提到的,我创建了一个更详细的博客文章here。如果您需要更多信息,请发表评论:)。

【讨论】:

  • 什么是 AMS 应用程序 id uri?我仍然无法让这个工作。我 99% 确定我遵循了您正在执行的所有相同步骤。其他问题是我输入的。您发送到端点的 HTTP 帖子是否包含身份验证标头?
  • AMS App ID URI 应该是 https://.azure-mobile.net/login/aad 这是应该在 AAD 注册的“配置”选项卡中设置的值移动服务。
  • @Sven - 你能在准备好后用你的 Angular 模块的位置更新这篇文章吗?此外,如果您可以共享 getAADToken() 的定义,那就太好了。我的印象是您只能从 ADAL JS 中获取 ID 令牌 - 很高兴知道如何获取访问令牌。
  • @mattchenderson 我将在 Github 上发布一个 Angular Modul,并在我的博客中写一篇关于它的文章,提供更多详细信息。我将在 cmets 中发布 URL,我也会用所有必要的细节更新我的答案。 getAADToken 使用 ADAL.JS 获取 Azure 移动服务的令牌。这就是我将 HTML AAD 应用程序配置为访问 Azure 移动应用程序的原因。我会尽量在星期五之前发布更多内容。
  • @PilotBob 希望此更新能帮助您解决问题
【解决方案2】:

POST 可能会返回 401,因为 AAD 令牌的受众不正确。移动服务希望这是它的 /login/aad 端点,但我怀疑您发送的令牌实际上是在您调用的网站范围内。委托访问权限只是说您可以从站点获取令牌并将其转换为移动服务的令牌。它不会改变已发行令牌本身的性质。

因此,最好的建议是确保您登录到移动服务受众,或执行委派的访问流程。不幸的是,后者unless using ADAL.NET的样本似乎并不多

一种解决方法是在移动服务上设置 MS_AadAudience 应用设置以匹配您网站的设置。仅当站点和移动服务存在于您的应用程序的相同逻辑安全边界内时,您才应该这样做。也就是说,任何可以登录到您网站的东西都可以在此时访问移动服务。总的来说,更好的方法是获取移动服务的访问令牌。

【讨论】:

    【解决方案3】:

    您可以使用 AzureMobileServices 客户端脚本使用已获得的令牌进行登录:

    您需要包含以下脚本: https://ajax.aspnetcdn.com/ajax/mobileservices/MobileServices.Web-1.2.8.min.js

    那么当你通过 ADAL.JS 获取到 token 后,你就可以使用它来登录并获取 Mobile Service Authentication Token:

    var appUrl = 'https://foobar.azure-mobile.net'
      , appKey = 'zumo key' // found on the dashboard of the mobile service
      , client = new WindowsAzure.MobileServiceClient(appUrl, appKey);
    
    // ...
    var token = this.getAADToken(ZUMOAuthenticationProvider.Config().url);
    
    client
      .login('aad', { 'access_token': token })
      .then(function() {
        // client.currentUser.mobileServiceAuthenticationToken
      });
    

    然后需要将此令牌包含在后续的移动服务 API 请求中:

    var config = {
      headers: {
        'X-ZUMO-AUTH': client.currentUser.mobileServiceAuthenticationToken
      }
    }
    
    $http
      .get(appUrl + '/some/path', config)
      .then(function (r) {
         console.log(r);
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-26
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      相关资源
      最近更新 更多