Office 365 登录时未保存用户信息 (ADAL.js)

No User Info saved with Office 365 Login (ADAL.js)

我正在使用 ADAL.js 库通过 Office 365 登录验证我的 Excel 加载项。为此,我正在使用 Azure AD 应用程序,并且也授予了所需的权限。我使用 ADAL.js 的设置如下:

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

登录正常。它正确重定向到加载项主页,但无法使用 getCachedUser 函数检索用户信息。我得到的只是一个 null 值。我是不是做错了什么?

Microsoft 建议使用 office-js-helpers 通过隐式流程授权外部服务,而不是使用 adal 库。

这是使用 Azure AD 应用进行身份验证的代码 spinet:

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

关于这个库的更多细节,你可以参考this link。以下文档也有助于在 Office 加载项中进行授权:

Authorize external services in your Office Add-in