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
            );
}]);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐