Search code examples
azureazure-active-directoryoffice365-appsadal.js

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


I am using ADAL.js library for authenticating my Excel add-in via Office 365 Login. I am using an Azure AD application for this and have granted the required permissions too. The settings I have used with ADAL.js are below:

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

The login works fine. It redirects properly to the add-in homepage but the user information is not retrievable using the getCachedUser function. All I get is a null value. Am I doing something wrong here?


Solution

  • Instead of using the adal library, Microsoft recommend using the office-js-helpers to authorize external services with the implicit flow.

    Here is a code spinet to authenticate with Azure AD app:

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

    Authentication

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

    Getting a cached token

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

    More detail about this library, you can refer this link. And below document is also helpful about authorize in Office add-in:

    Authorize external services in your Office Add-in