2017-10-18 52 views
1

我們遇到了以下問題: 我們已經實現了Azure AD認證的Web API,並且我們已成功使用ADAL 1.0.12調用它(我們有自己的包裝來處理iframe以進行無聲登錄)。ADAL.js令牌更新操作由於超時而失敗Azure AD Auth Web API

現在我們擺脫了所有的包裝,並升級到最新版本,並開始得到這裏描述的問題:令牌更新操作超時。

API背後的Azure AD架構非常簡單:已註冊的具有讀取目錄數據和登錄並讀取用戶配置文件權限的Web應用程序。我們有另一個本地應用程序註冊了Web應用程序的權限(訪問Web APP NAME HERE)。這兩個應用程序都在其清單中將oauth2AllowImplicitFlow設置爲true。

兩個應用程序都註冊了https://ourtenant.sharepoint.com/ *的通配符重定向URI(我們正在從SharePoint調用Web API)。此外,本地客戶端應用程序已將重定向URI註冊到Web API應用程序的唯一URI(應用程序ID URI)。

這兩個應用程序已被授予管理員同意之前 - 他們工作的罰款與舊版本的ADAL.js

所以,這裏是一些代碼。請注意,我們已經嘗試了沒有displayCall回調(直接在當前頁面刷新頁面)和頁面刷新(在initLogin方法中檢查註釋的代碼)。我們還有一個開關,用於在成功登錄時生成彈出窗口或帶有回調的iframe。

問題出在authContext.acquireToken。請注意,如果我們調用OurNamespace.authContext.getCachedToken(OurNamespace.clientId),我們將獲得資源的存儲令牌。

請注意,我們正在每個頁面刷新/ iframe/popup後調用正確的handleWindowCallback。

無論瀏覽器如何,都會發生。

OurNamespace = { 

serviceMainUrl: "https://localhost:44339", 
webApiAppIdUri: "WEB-API-URI", 
tenant: "TENANT", 
clientId: "NATIVE-APP-GUID", // Native APP 
adalEndPoints: { 
    get: null 
}, 
adal: null, 
authContext: null, 
dummyAuthPage: null, 
usePopup: true, 

init: function() { 
    this.dummyAuthPage = "DummmyLogin.aspx"; 
    OurNamespace.adalEndPoints.get = OurNamespace.serviceMainUrl + "/api/values"; 
    OurNamespace.authContext = new AuthenticationContext({ 
     tenant: OurNamespace.tenant + '.onmicrosoft.com', 
     clientId: OurNamespace.clientId, 
     webApiAppIdUri: OurNamespace.webApiAppIdUri, 
     endpoints: OurNamespace.adalEndPoints, 
     popUp: false, 
     postLogoutRedirectUri: window.location.origin, 
     cacheLocation: "localStorage", 
     displayCall: OurNamespace.displayCall, 
     redirectUri: _spPageContextInfo.siteAbsoluteUrl + "/Pages/" + OurNamespace.dummyAuthPage 
    }); 
    var user = OurNamespace.authContext.getCachedUser(); // OurNamespace.authContext.getCachedToken(OurNamespace.clientId) 
    if (user) { 
     OurNamespace.azureAdAcquireToken(); 
    } else { 
     OurNamespace.initLogin(); 
    } 
}, 

initLogin: function() { 
    //OurNamespace.authContext.config.displayCall = null; 
    //var isCallback = OurNamespace.authContext.isCallback(window.location.hash); 
    //OurNamespace.authContext.handleWindowCallback(); 

    //if (isCallback && !OurNamespace.authContext.getLoginError()) { 
    // window.location.href = OurNamespace.authContext._getItem(OurNamespace.authContext.CONSTANTS.STORAGE.LOGIN_REQUEST); 
    //} 

    OurNamespace.authContext.login(); 
}, 

displayCall: function (url) { 
    var iframeId = "azure-ad-tenant-login", 
     popup = null; 
    if (OurNamespace.usePopup) { 
     popup = window.open(url, 'auth-popup', 'width=800,height=500'); 
    } else { 
     var iframe = document.getElementById(iframeId); 
     if (!iframe) { 
      iframe = document.createElement("iframe"); 
      iframe.setAttribute('id', iframeId); 
      document.body.appendChild(iframe); 
     } 
     iframe.style.visibility = 'hidden'; 
     iframe.style.position = 'absolute'; 
     iframe.src = url; 
     popup = iframe.contentDocument; 
    } 
    var intervalId = window.setInterval(function() { 
     try { 
      // refresh the contnetDocument for iframe 
      if (!OurNamespace.usePopup) 
       popup = iframe.contentDocument; 
      var isCallback = OurNamespace.authContext.isCallback(popup.location.hash); 
      OurNamespace.authContext.handleWindowCallback(popup.location.hash); 
      if (isCallback && !OurNamespace.authContext.getLoginError()) { 
       popup.location.href = OurNamespace.authContext._getItem(OurNamespace.authContext.CONSTANTS.STORAGE.LOGIN_REQUEST); 
       window.clearInterval(intervalId); 
       if (OurNamespace.usePopup) { 
        popup.close(); 
       } 
       var user = OurNamespace.authContext.getCachedUser(); 
       if (user) { 
        console.log(user); 
       } else { 
        console.error(OurNamespace.authContext.getLoginError()); 
       } 
      } 
     } catch (e) { } 
    }, 400); 
}, 

azureAdAcquireToken: function() { 
    OurNamespace.authContext.acquireToken(OurNamespace.adalEndPoints.get, function (error, token) { 
     if (error || !token) { 
      SP.UI.Status.addStatus("ERROR", ('acquireToken error occured: ' + error), true); 
      return; 
     } else { 
      OurNamespace.processWebApiRequest(token); 
     } 
    }); 
}, 

processWebApiRequest: function (token) { 
    // Alternatively, in MVC you can retrieve the logged in user in the web api with HttpContext.Current.User.Identity.Name 
    $.ajax({ 
     type: "GET", 
     url: OurNamespace.adalEndPoints.get, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     data: {}, 
     headers: { 
      'Authorization': 'Bearer ' + token 
     }, 
     success: function (results) { 
      var dataObject = JSON.parse(results); 
      SP.UI.Status.addStatus("Info", "ADAL GET data success: " + dataObject.webApiUser); 
      $(".grid-info").html("<h1 class=\"h1\">Current Web API authenticated user: " + dataObject.webApiUser + "</h1>"); 
     }, 
     error: function (xhr, errorThrown, textStatus) { 
      console.error(xhr); 
      SP.UI.Status.addStatus("ERROR", ('Service error occured: ' + textStatus), true); 
     } 
    }); 
} 
} 

回答

0

我使用authContext.acquireToken實際上調用this._renewToken(resource, callback)通過隱藏iframe來獲得訪問令牌使用1.0.15 adal.js並獲得訪問令牌測試成功。這是給你參考的完整的測試示例代碼:

<html> 
 
<head> 
 

 
<script src="/js/1.0.15/adal.js"></script> 
 
<script> 
 

 
var config = { 
 
    tenant: 'adfei.onmicrosoft.com', 
 
    clientId: '7e3b0f81-cf5c-4346-b3df-82638848104f', 
 
    redirectUri: 'http://localhost/spa.html', 
 
    navigateToLoginRequestUrl:false, 
 

 
}; 
 

 
var authContext=new AuthenticationContext(config); 
 

 
var hash = window.location.hash; 
 

 
if(hash.length!=0){ 
 
    authContext.handleWindowCallback(hash); 
 
    var user=authContext.getCachedUser(); 
 
} 
 
    
 

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

 
function acqureToken(){ 
 
    authContext.acquireToken("https://graph.microsoft.com", function(error, token, message){ 
 
     console.log(token) 
 

 
    }) 
 
} 
 

 
</script> 
 
</head> 
 

 
<body> 
 
<button onclick="login()">Login</button> 
 
<button onclick="acqureToken()">AcquireToken</button> 
 

 
</body> 
 
</html>

是否有幫助?或者你會介意共享這個問題的可運行的代碼示例?

0

我不確定您使用的adal.js版本是否正確。但是可以在毫秒中設置配置對象的loadFrameTimeout設置。檢查你的adal.js文件的頂部,它應該在那裏。

相關問題