尝试在Javascript中实现ADAL(Azure AD),不断获得登录/重定向循环

尝试在Javascript中实现ADAL(Azure AD),不断获得登录/重定向循环,javascript,azure-active-directory,Javascript,Azure Active Directory,所以我必须用html创建一个日历,从Outlook获取事件,然后将其作为自定义页面部署到Sharepoint,这样它就可以作为Web部件/iframe包含在网站集中 问题是,我尝试添加ADAL安全性,因为您需要登录并向Microsoft Exchange Online API发送令牌,以便获取日历事件等。要显示日历部分,我使用FullCalendar.io 现在我一直在得到一个永不结束的登录/重定向循环。有人看到代码中的错误吗?这是: var $this = this; $(document)

所以我必须用html创建一个日历,从Outlook获取事件,然后将其作为自定义页面部署到Sharepoint,这样它就可以作为Web部件/iframe包含在网站集中

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

现在我一直在得到一个永不结束的登录/重定向循环。有人看到代码中的错误吗?这是:

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);
    });
  }
});
 }  
因此,代码会先获取“acquire token”,然后是最后一个“else”,因此会调用“$this.DisplayEvents(token)”!然而,在获得令牌后,应用程序将永远保持重定向。。。我的Azure AD App注册中的回复URL也是window.config重定向URL值,否则我会收到一个错误,说明回复URL在请求和Azure之间不匹配


有人知道哪里出了问题吗

我可以使用您的代码重现您的问题。若使用authContext.getCachedUser()检查登录状态,重定向问题将消失

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