question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

acquireTokensilent return null accessToken randomly.

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:42 (13 by maintainers)

github_iconTop GitHub Comments

5reactions
raikosscommented, Jun 19, 2019

@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.

3reactions
pkanher617commented, Jun 18, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Azure - AD - AcquireTokenSilent giving error ... - Stack Overflow
When the AccessToken expires, it throws an exception and it tries to ... AuthenticationType); (return null from controller's action as the ...
Read more >
acquireTokenSilent() returns an access token signed by the ...
I have an SPA that uses MSAL (msal-browser@2.7.0) to authenticate against Azure AD B2C. I call loginRedirect() and everything works as ...
Read more >
acquiretokensilent refresh token - You.com | The AI Search ...
MSAL access token refreshing not working through AcquireTokenSilent. Asked Feb 20, 2022 • 0 votes 1 answer ... return null accessToken randomly.#736.
Read more >
Adding MSAL And React - Azure Serverless Quickstart - GitBook
var authResult : msal.AuthenticationResult;. try {. authResult = await msalInstance.acquireTokenSilent(silentRequest). return authResult.accessToken;. } ...
Read more >
Msal acquiretokensilent refresh token angular
If the access token is expired but the refresh token is still valid, MSAL will use the given refresh token to retrieve a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found