【问题标题】:AngularJS + ADAL.JS set Resource ID (Audience)AngularJS + ADAL.JS 设置资源 ID(受众)
【发布时间】:2015-07-09 06:05:18
【问题描述】:

如何在 AngularJS 中使用 adal.js 从我的 javascript 代码中为观众 https://management.azure.com 获取不记名令牌?

我在 AD 中创建了一个客户端应用程序并设置其权限以允许它访问“Windows Azure 服务管理 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/'.

【问题讨论】:

    标签: javascript angularjs azure azure-active-directory adal.js


    【解决方案1】:

    好的,我在这里浏览了 ADAL.JS 的源代码后找到了解决方案。在第 137 行,它查看 config.loginResource 以查看在将 config 对象传递给 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
                );
    }]);
    

    【讨论】:

    • 请问你是怎么做到的?我想知道您为什么不在 adalService.init 中使用端点?
    • 它通过了身份验证,但现在陷入无限循环或更新令牌。
    • @Jaanus 我遇到了同样的问题。如果你等待的时间足够长,你会发现它不是一个无限循环。一段时间后它会停止刷新。可悲的是,我没有弄清楚为什么会发生这种情况。
    猜你喜欢
    • 2022-06-13
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2015-04-26
    • 2017-12-16
    • 2019-08-15
    相关资源
    最近更新 更多