javascript – AngularJS ADAL.JS设置资源ID(受众)

如何在AngularJS中使用adal.js从我的javascript代码中获取受众https://management.azure.com的持有人令牌?

我在AD中创建了一个客户端应用程序,并设置其权限以允许它访问“Windows Azure Service Management API”.我的angularjs代码如下:

adalService.init(
            {
                instance: "https://login.windows.net/",
                tenant: "<something>.onmicrosoft.com",
                clientId: "<some id>",
                cacheLocation: 'localStorage',
                redirectUri: 'http://localhost:63691/index.html#/configure',
                endpoints: {
                    /* 'target endpoint to be called': 'target endpoint's resource ID' */
                    'https://management.azure.com/subscriptions?api-version=2014-04-01': 'https://management.azure.com/'
                }
            },
            $httpProvider
        );

如果我在POSTMAN中使用此adalService收到的令牌来调用https://management.azure.com/subscriptions?api-version=2014-04-01,则会收到以下错误:

The access token has been obtained from wrong audience or resource '<some id>'. 
It should exactly match (including forward slash) with one of the allowed audiences 'https://management.core.windows.net/','https://management.azure.com/'.

解决方法:

好的,所以我在这里查看了ADAL.JS的源代码后找到了解决方案.在第137行,它查看config.loginResource以查看在将配置对象传递给init()函数时是否已设置它.

把它放在那里让任何人卡住:

如果您需要令牌来声明“https://management.azure.com/”(或任何其他资源URI),您可以在初始化AuthenticationContext时设置受众,如下所示:

app.config(['$routeProvider', '$httpProvider', 'adalAuthenticationServiceProvider', function ($routeProvider, $httpProvider, adalService) {
    adalService.init(
                {
                    instance: "https://login.microsoftonline.com/",
                    tenant: "<something>.onmicrosoft.com",
                    clientId: "<client-id>",
                    cacheLocation: 'localStorage', //optional
                    redirectUri: '<redirect-uri>',
                    loginResource: 'https://management.azure.com/' //to set AUDIENCE
                },
                $httpProvider
            );
}]);

相关文章

Microsoft云包括了Azure、PowerPlatform、Microsoft365、Git...
《WindowsAzurePlatform系列文章目录》 我们在使用AzureAPI...
微软免费使用一年的Azure虚拟机,默认提供了一个64G的磁盘,...
上篇请访问这里做一个能对标阿里云的前端APM工具(上)样本多...
一年一度的MicrosoftBuild终于来了,带来了非常非常多的新技...