Solvedmicrosoft authentication library for js acquireTokensilent return null accessToken randomly.

I'm submitting a...

[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ x] Other... Please describe:
Technical doubt and possibly a bug.

Browser:

  • [ X] Chrome
  • Firefox version XX
  • IE version XX
  • Edge version XX
  • Safari version XX

Library version

<
Library version: 1.0.0

Current behavior

accessToken from acquireTokenSilent is null sometimes

Expected behavior

accessToken from acquireTokenSilent is never null. Any error should come in the error function.

Minimal reproduction of the problem with instructions

I have an apihelper that every time is going to do a call contacts my clientApplication wrapper like this:

 getAuthorizationCookie = async function () {
        var token = await Ab2c.getTokenAsync()
        if (!token)
        {
            console.log(token);
        }
        return 'Bearer ' + token;
    }

The implementation of Ab2c.getTokenAsync is this:

let authCallback = function (error, response) {
  if (error)
    console.log(error);
};
const clientApplication = new UserAgentApplication(config);
clientApplication.handleRedirectCallback(authCallback);
//FIXME: this temporary catch token is wrong and should be  removed. It is here as a workaround because ab2c to sometimes returns a valid response with a null token.
let tempCacheToken="";**

class Ab2c {
....

static getTokenAsync = async () => {
    let tokenRequest = {
      scopes: config.auth.b2cScopes,
      authority: config.auth.authority,
      extraQueryParameters: config.auth.extraQueryParameters
    };
    return new Promise((resolve,reject) =>{
    clientApplication.acquireTokenSilent(tokenRequest).then(function (loginResponse) {
      if (!loginResponse.accessToken)
      {
         **//This If happens sometimes when I refresh the page**
          console.log("null token");
          loginResponse.accessToken = tempCacheToken;
      }
      else
      {
             console.log("valid token")
          tempCacheToken = loginResponse.accessToken;
      }
      return resolve(loginResponse.accessToken);
    }).catch(function (error) {
      console.log(error);
      Ab2c.logout()
      let newError = new Error(error);
      newError.fromMsal = true;
      return reject(newError);
    });
    });
  }
....
}

So, basically I'm relying in clientApplication to handle the cached token for me since I understood that's what I'm supposed to do when using msal. But sometimes when I hit F5 in my page I get a valid loginResponse with a null accessToken.

When I refresh I do like 4 or 5 different requests which means 4 or 5 different calls to acquiretokensilent. What I see is that sometimes all of them work and sometimes the first actually works and returns a token but the following return a null value, most of the times all of them work...
Log example.
valid token null token valid token valid token valid token valid token
I think it might be related to refreshing while some request is still pending but I can't prove it.

Any ideas?

Miguel

43 Answers

✔️Accepted Answer

@mmuarc What I did was:

  1. Clone the project
  2. Switch to the branch
  3. Run npm install and npm run build in the /lib/msal-core folder
  4. In my project where I use msal, run npm uninstall msal to remove the npm hosted package
  5. Run npm install file:<path_to_msal-core_folder> to install the package locally

Though this may be different than what others may do, this is what enabled me to test the new version.

Don't forget to remove the reference to the local package by running npm uninstall msal and then npm install msal again when you are done testing.

Hope this was helpful to you.

Other Answers:

@mmuarc @raikoss @sfigie Thanks for your patience. I have pushed a fix in PR #768. Please feel free to pull this branch and let me know if this fix works. I will push this as a patch as soon as I have verified that it works for you.

Related Issues:

28
microsoft authentication library for js Invalid Issuer (iss: sts.windows.net) when acquiring token
I believe this is a configuration issue in the Azure AD App Registration I'm submitting a.. ...
15
microsoft authentication library for js B2C Token endpoint CORS
We appreciate everyone's patience I certainly understand the frustration Please follow the issue tem...
12
microsoft authentication library for js Getting A silent sign-in request was sent but no user is signed in in multiple app sign-in environment
@jmckennon . Might not be related to this but after this issue is been closed Please follow the issu...
9
microsoft authentication library for js Sample Code for Angular 8 (msal-angular)
@stevenruizhang @jaleleddineagrebi @Choran66 There is a new beta for MSAL Angular with rxjs 6 suppor...
7
microsoft authentication library for js Preventing 3rd Party Cookies block when using MSAL Angular (1.x)
@jo-arroyo Thanks for getting back to me This is a big issue for us Looking at the MSAL.js 2.0 sampl...
5
microsoft authentication library for js acquireTokensilent return null accessToken randomly.
@mmuarc What I did was: Clone the project Switch to the branch Run npm install and npm run build in ...
4
microsoft authentication library for js User cannot log out
For anyone who is stumped on this and finds this thread I found a solution I'm submitting a.. ...