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

时间:2017-07-03 06:27:52

标签: azure azure-active-directory office365-apps 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值。我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

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

以下是使用Azure 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');

有关此库的更多详细信息,请参阅this link。以下文档也有助于在Office加载项中进行授权:

Authorize external services in your Office Add-in