使用ADAL.js获取令牌对Azure移动服务应用进行身份验证

Sve*_*ach 6 c# mobile azure azure-mobile-services adal

我正在尝试针对Azure移动服务应用验证HTML应用.

安装程序

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

Azure移动服务应用:

HTML应用:

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

Azure移动服务使用.NET后端,我在其中包含和配置了NuGet包"Microsoft Azure移动服务.NET后端安全扩展",如https://azure.microsoft.com/en-gb/documentation/articles/中所述.移动服务- DOTNET -后端窗口电话-GET-开始用户/

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
);
Run Code Online (Sandbox Code Playgroud)

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

问题

Azure移动服务不接受承载令牌 - 我获得401未经授权.我不知道为什么,但Azure移动服务使用它自己的身份验证标头 - 但没关系.

MSDN为Azure移动服务定义了一个所谓的"客户端定向登录操作":

"通过使用已从身份提供商处获得的身份令牌,从Microsoft Azure Mobile Services请求身份验证令牌." (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);
       }); 
Run Code Online (Sandbox Code Playgroud)

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

此POST请求也会收到401响应.所以我不知道如何验证我的应用程序.我也尝试了天蓝色的移动服务js lib.这个lib工作正常,但它使用弹出窗口进行身份验证,但我不想在我的项目中添加另一个库来进行一些REST调用.

类似的问题

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

为什么我的Azure移动服务不接受ADAL.js发送的承载令牌?

  • 同样的问题,没有解决方案(甚至在最后评论中链接的聊天记录中)

如何使用Azure AD保护Azure移动服务?ADAL.JS

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

我还看了一下来自新Azure管理门户的新Azure Mobile应用程序,但似乎他们使用相同的身份验证机制.

那么,我怎样才能使这个工作?

Sve*_*ach 2

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

endpoints: {
    '<AMS app client id>': 'https://ampapp.azure-mobile.net/'
}
Run Code Online (Sandbox Code Playgroud)

这应该是

endpoints: {
    'https://ampapp.azure-mobile.net/': '<AMS app id uri>': 
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:

正如这里所承诺的更详细的答案:

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

  • 适用于 Azure 移动服务的 AAD 应用程序
  • 适用于 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
);
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用客户端定向的登录操作来获取 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);
                });
});
Run Code Online (Sandbox Code Playgroud)

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