2017-07-03 91 views
1

我使用ADAL.js庫用於驗證我的Excel插件通過Office 365的登錄。我爲此使用了Azure AD應用程序,並且也授予了所需的權限。我使用的ADAL.js設置如下:沒有用戶信息保存與Office 365登錄(ADAL.js)

var config = { 
    tenant: tenant, 
    clientId: clientId, 
    redirectUri: redirectUrl, 
    postLogoutRedirectUri: logoutUrl, 
    extraQueryParameter: 'scope=openid+profile', 
    cacheLocation: 'localStorage' 
}; 

登錄工作正常。它正確地重定向到加載項主頁,但不能使用getCachedUser函數檢索用戶信息。我得到的是一個null值。我在這裏做錯了什麼?

回答

0

除了使用阿達爾庫,微軟建議使用office-js-helpers授權與隱含流動外部服務。

下面是一個代碼尖晶石與天青AD應用來驗證:

var authenticator = new OfficeHelpers.Authenticator(); 

// register Microsoft (Azure AD 2.0 Converged auth) endpoint using 
authenticator.endpoints.registerMicrosoftAuth('client id here'); 

// register Azure AD 1.0 endpoint using 
authenticator.endpoints.registerAzureADAuth('client id here', 'tenant here'); 

認證

// for the default AzureAD endpoint 
authenticator 
    .authenticate(OfficeHelpers.DefaultEndpoints.AzureAD) 
    .then(function (token) { /* Microsoft Token */ }) 
    .catch(OfficeHelpers.Utilities.log); 

獲得一個高速緩存的令牌

authenticator 
    .authenticate('name of endpoint') 
    .then(function(token) { 
    /* 
     `token` is either cached or newly obtained upon expiry. 
    */ 
    }) 
    .catch(OfficeHelpers.Utilities.log); 

authenticator 
    .authenticate('name of endpoint', true /* force re-authentication */) 
    .then(function(token) { 
    /* 
     `token` is newly obtained. 
    */ 
    }) 
    .catch(OfficeHelpers.Utilities.log); 

// get the cached token if any. returns null otherwise. 
var token = authenticator.tokens.get('name of endpoint'); 

更多細節約THI您可以參考this link。以下文件也有利於有關授權的Office加載項:

Authorize external services in your Office Add-in

+0

這似乎是最接近我預想的解決方案。有沒有辦法獲得用戶的電子郵件? – immysl

+1

@immysl AFAIK,這個庫不支持獲取用戶的郵件(UPN?)。作爲一種解決方法,您可以通過解碼令牌來獲得它。 –

+0

謝謝。我會試試:) – immysl