【问题标题】:Acquiring access token using AuthenticationContext of ADAL使用 ADAL 的 AuthenticationContext 获取访问令牌
【发布时间】:2017-09-01 09:27:32
【问题描述】:

我正在使用 ADAL.js 获取 Azure 资源的令牌。

<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.15/js/adal.min.js"></script>

为此,我编写了以下代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.15/js/adal.min.js"></script>
    <script>
        var endpoints = {
            "https://management.core.windows.net": "https://management.core.windows.net"
        };
        var config = {
            clientId: 'e333d3fe-a73a-4476-8121-8a57f9a972ca',
            endpoints: endpoints,
        };
        var authContext = new AuthenticationContext(config);
        authContext.handleWindowCallback();

        function login() {
            authContext.popUp = true;
            authContext.login();
           // authContext.handleWindowCallback();
            var user = authContext.getCachedUser();
            console.log(user);
        };

        function clickme() {
            var user = authContext.getCachedUser();
            console.log(user);

            authContext.acquireToken('https://management.core.windows.net', function (error, token) {
                console.log(error);
                console.log(token);
                ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                if (!!error) {
                    console.log(error.indexOf("interaction_required"));
                    authContext.acquireTokenPopup(
                        'https://management.core.windows.net/',
                        null,
                        null,
                        function (error, token, msg) {
                            console.log(error);
                            console.log(token);
                        }
                    )
                }
                +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            });
        };

        function logout () {
            authContext.logout();
        };

    </script>
    <input id="Button1" type="button" value="clickme" onclick="clickme()" />
    <input id="Button3" type="button" value="login" onclick="login()" />
    <input id="Button2" type="button" value="logout" onclick="logout()" />

    // These are the text-boxes whose value I want to retain.
    First name:<br>
    <input id=fname" type="text" name="firstname" value="Mickey">
    <br>
    Last name:<br>
    <input id="lname" type="text" name="lastname" value="Mouse">
</body>
</html>

上面的代码有两个问题:

  1. 当我调用 authContext.acquireToken() 时:出现以下错误:AADSTS50079:用户需要使用多重身份验证。 authContext.login() 函数只要求提供一次凭据,并且不要求进行多因素身份验证。我该如何处理?
  2. 当我们单击注销时,fname 文本框和 lname 文本框的值会随着页面刷新而丢失。如何避免重新加载页面?
  3. 我已根据答案更新了代码,但收到以下错误消息:弹出窗口为空。如果您使用的是 IE,就会发生这种情况。我在这里做错什么了吗??我正在使用铬。 由于弹出窗口在 chrome 中被阻止,因此出现以下问题。允许弹出后,代码工作正常。

【问题讨论】:

  • 对于注销,必须向 AAD 端点发出请求,该端点将处理适当的会话 cookie 的清除。我认为不能避免页面刷新,除非有办法通过从隐藏的 iframe 发出请求来避免它。

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


【解决方案1】:

错误 AADSTS50079: The user is required to use multi-factor authentication 意味着特定的最终用户必须执行或注册多重身份验证才能获得访问令牌。 acquireToken() 是静默请求,因此无法向最终用户显示 MFA UI。调用 login() 并不能解决此问题,因为您没有在登录调用中请求访问任何内容。

解决方法是捕获此错误:

if (error.indexOf("interaction_required") !== -1)

然后,您的应用可以使用acquireTokenPopup()acquireTokenRedirect(),它们是对同一资源的交互式请求。然后,这将提示您的最终用户完成 MFA 请求,您将获得访问令牌。

【讨论】:

  • 我试过这个:authContext.acquireToken('https://management.core.windows.net/', function (error, token) { console.log(error.indexOf("interaction_required")); }。在日志中打印了 -1。
  • 我已根据您的回答更新了代码和问题。请看看这个。
  • 错误信息:弹出窗口为空。如果您使用的是 IE,则可能会发生这种情况:即将出现,因为该特定站点“login.microsoftonline.com/common/oauth2......”的 chrome 上阻止了弹出窗口。
  • 仍然 error.indexOf("interaction_required") 仅对我来说是-1。
  • @AkashAgarwal 你能打开一个浏览器调试器,看看error 在这个案例中说了什么吗?您可能需要捕获其他错误。
猜你喜欢
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2018-04-24
  • 2018-04-08
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
相关资源
最近更新 更多