尝试在Javascript中实现ADAL(Azure AD),并不断获得登录/重定向循环 [英] Trying to implement ADAL (Azure AD) in Javascript, keep getting a login/redirect loop

查看:115
本文介绍了尝试在Javascript中实现ADAL(Azure AD),并不断获得登录/重定向循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我必须用html创建一个日历,该日历从Outlook获取事件,然后将其作为自定义页面部署到Sharepoint,以便可以将其作为webpart/iframe包含在网站集中.

So I've got to create a calendar in html that gets events from Outlook and then deploy that as a custom page to Sharepoint, so it can be included as a webpart/iframe in site collections.

问题是我已尝试添加ADAL安全性,因为您需要登录&发送令牌到Microsoft Exchange Online API以获得日历事件等.要显示日历部分,我正在使用FullCalendar.io.

The problem is that I've tried adding ADAL security because you need to be logged in & send a token to Microsoft Exchange Online API in order to get calendar events etc. To display the calendar part, I'm using FullCalendar.io .

现在,我一直在不断获得从未结束的登录/重定向循环.有人看到代码中的错误吗?在这里:

Now I've been keep getting a login/redirect loop that never ends. Does anyone see the fault in code? Here it is:

var $this = this;

$(document).ready(function() {
debugger;

window.config = {
      tenantId: {tenant},
      clientId: {clientid},
      popUp: true,
      callback: callbackFunction,
      redirectUri: {custom aspx page URL on our Sharepoint},
      cacheLocation: 'localStorage'
};

var authenticationContext = new AuthenticationContext(config);

authenticationContext.handleWindowCallback();

function callbackFunction(errorDesc, token, error, tokenType) {
  alert('callbackFunction reached!');
}
var items = null;

if (authenticationContext.TokenCache) {
  items = authenticationContext.TokenCache.ReadItems();
}

if (authenticationContext['_user']) {
  authenticationContext.acquireToken(config.clientId, function (errorDesc, token, error) {
    if (error) { //acquire token failure
      if (config.popUp) {
          // If using popup flows
          authenticationContext.acquireTokenPopup(config.clientId, null, null,  function (errorDesc, token, error) 
{});
      }
      else {
      // In this case the callback passed in the Authentication request constructor will be called.
          authenticationContext.acquireTokenRedirect(config.clientId, null, null);
      }
    }
    else {
      //acquired token successfully
      // alert('token success');
      $this.DisplayEvents(token);
    }
  });
}
else {
    // Initiate login
    authenticationContext.login();
}
 });

 function DisplayEvents(adalToken) {
$('#calendar').fullCalendar({
  header: {
    left: 'prev,next today',
    center: 'title',
    right: 'month,agendaWeek,agendaDay,listWeek'
  },
  navLinks: true, // can click day/week names to navigate views
  editable: true,
  eventLimit: true, // allow "more" link when too many events
  events: function(start, end, timezone, callback) {
    var headers = new Headers();
    var bearerToken = "Bearer " + adalToken;
    headers.append('Authorization', bearer);
    var options = {
      method: 'GET',
      headers: headers
    };
    var exchangeEndpoint = 'https://outlook.office.com/api/v2.0/me/events';

    fetch(exchangeEndpoint, options).then(function (response) {
      alert('Response data from successful call: ' + response);
    });
  }
});
 }  

因此代码确实获取了获取令牌",然后获取了最后一个"else",因此确实调用了"$ this.DisplayEvents(token)"!但是,获取令牌后,该应用程序将一直重定向到永远……我的Azure AD应用程序注册中的Reply URL也是window.config redirectURL值,否则我将收到一条错误消息,指出答复URL不在请求和Azure之间匹配.

So the code does get to "acquire token" and then the last "else", so "$this.DisplayEvents(token)" does get called! However, after acquire token, the app just keeps redirecting forever and ever... The Reply URL in my Azure AD App registration is also the window.config redirectURL value, or else I'd get an error stating the reply URL's don't match between request and Azure.

有人知道哪里出了问题吗?

Does anyone know where it's going wrong?

推荐答案

我可以通过使用您的代码来再现您的问题.如果您使用authContext.getCachedUser()检查登录状态,则重定向问题将消失.

I can reproduce your issue on my side by using your code. If you use authContext.getCachedUser() to check login status, redirect issue will disappear.

if (authContext.getCachedUser()) {
        authContext.acquireToken(config.clientId, function (error, token) {
            if (error) { //acquire token failure
                if (config.popUp) {
                    // If using popup flows
                    authContext.acquireTokenPopup(config.clientId, null, null, function (errorDesc, token, error) { });
                }
                else {
                    // In this case the callback passed in the Authentication request constructor will be called.
                    authContext.acquireTokenRedirect(config.clientId, null, null);
                }
            }
            else {
                //acquired token successfully
                // alert('token success');
                alert(token);
            }
        });
    }
    else {
        // Initiate login
        authContext.login();
    }

这篇关于尝试在Javascript中实现ADAL(Azure AD),并不断获得登录/重定向循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆