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

标签 javascript azure-active-directory

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

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

现在我一直在获取一个永无止境的登录/重定向循环。有没有人看到代码中的错误?在这里:

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

所以代码确实到达“获取 token ”,然后是最后一个“else”,所以“$this.DisplayEvents(token)”确实被调用了!但是,在获取 token 后,该应用程序将永远重定向...我的 Azure AD 应用程序注册中的回复 URL 也是 window.config redirectURL 值,否则我会收到一条错误消息,指出回复 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();
    }

关于javascript - 尝试在 Javascript 中实现 ADAL (Azure AD),不断出现登录/重定向循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54984229/

相关文章:

asp.net-mvc - 使用WindowsAzureActiveDirectoryBearer身份验证和应用程序级凭据

javascript - 如何解析逗号后的3位 float ?

javascript - Angular2 - 删除所有/最近的子组件后无法添加子组件

javascript - 如何用纯色图像覆盖整个网页(在顶部)

javascript - 如何: Print Select Option Values?

Azure AD 注册的应用程序不在同意屏幕上显示 ToS/隐私链接

javascript - 如果原始 src 的高度/宽度小于 "x"像素,是否更改图像 src?

azure-active-directory - 如何以编程方式指定 replyUrlsWithType

azure - 无法使用系统分配的托管标识 ID 登录 Azure

azure - 如何使用 Azure Ad client_credentials 对服务器到服务器通信的后端 Web api 进行身份验证