microsoft-authentication-library-for-js: acquireTokenSilent doesn't renew the id-token
Core Library
MSAL.js v2 (@azure/msal-browser)
Core Library Version
2.18.0
Wrapper Library
Not Applicable
Wrapper Library Version
None
Description
Hi Micrsoft communiy,
We have a react app, which implements your library as described below. We only use it to get an id-token for validating the Microsoft account on our server. Unfortunately, after an hour the id-tokens that are returned by the acquireTokenSilent function are expired.
We have tried to set tokenRenewalOffsetSeconds to 300, but this doesn’t resolve the issue. If we logout, clear the cache or refresh the site a few times, we get a valid token again. But this is unfortunately, very unpleasant for our users.
We get a lot of customer complaints on a daily basis, because of this issue. Please help us. We are thankful for any suggestions. Thanks!!
Error Message
No response
Msal Logs
Here are some logs. Please ignore the “DEBUG” in front of the messages.:
DEBUG | Message: [Mon, 01 Nov 2021 16:08:42 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:handleRedirectStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:08:42 GMT] : [5c24a3aa-1095-42c9-85a2-d71a097390a4] : msal.js.browser@2.18.0 : Info - handleRedirectPromise called but there is no interaction in progress, returning null. log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:08:42 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:handleRedirectEnd log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:08:42 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:08:42 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenSuccess log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:08:42 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:08:42 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenSuccess log.js:16:10
// we get an expired id-token
// we refresh the page
// we get the same logs as above and get an expired id-token
// we refresh the page again
DEBUG | Message: [Mon, 01 Nov 2021 16:09:53 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:handleRedirectStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:53 GMT] : [2e893bd0-832f-4730-8cae-22e5108dada7] : msal.js.browser@2.18.0 : Info - handleRedirectPromise called but there is no interaction in progress, returning null. log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:53 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:handleRedirectEnd log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:53 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenFromNetworkStart log.js:16:10
Some cookies are misusing the recommended “SameSite“ attribute 2
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenSuccess log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenSuccess log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenSuccess log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenSuccess log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenStart log.js:16:10
DEBUG | Message: [Mon, 01 Nov 2021 16:09:54 GMT] : @azure/msal-browser@2.18.0 : Info - Emitting event: msal:acquireTokenSuccess
// we suddenly get a valid id-token and everything works again as expected
MSAL Configuration
{
auth: {
authority: 'https://login.microsoftonline.com/common',
clientId: <client-id>,
postLogoutRedirectUri: window.location.origin,
redirectUri,
validateAuthority: true,
navigateToLoginRequestUrl: false,
},
system: {
loggerOptions: {
loggerCallback: (level, message, containsPii) => {
console.log(message);
},
piiLoggingEnabled: false // disables personal information
},
windowHashTimeout: 60000,
iframeHashTimeout: 10000,
loadFrameTimeout: 0,
// I also tried: tokenRenewalOffsetSeconds: 300
},
cache: {
cacheLocation: 'localStorage',
storeAuthStateInCookie: true
}
}
Relevant Code Snippets
const msalScopes = [ 'openid', 'User.Read' ];
const graphScopes = [ ];
const config = ... // see above
const msalClient = new PublicClientApplication(config);
// login (not essential):
const login = async () => {
const accounts = msalClient.getAllAccounts();
if (accounts || accounts.length < 1) {
let tokenResponse = await msalClient.handleRedirectPromise();
const accountObj = tokenResponse
? tokenResponse.account
: msalClient.getAllAccounts()[0];
if (!tokenResponse) {
if (accountObj) {
// User has logged in, but no tokens:
try {
tokenResponse = await msalClient.acquireTokenSilent({
account: msalClient.getAllAccounts()[0],
scopes: msalScopes,
});
} catch (err) {
await msalClient.acquireTokenRedirect({ scopes: msalScopes });
}
} else {
// No accountObject or tokenResponse present. User must now login:
await msalClient.loginRedirect({ scopes: msalScopes });
}
}
}
}
// get id-token (here is the problem):
async getIdToken(scopes=null) {
const accounts = msalClient.getAllAccounts();
const { idToken } = await msalClient.acquireTokenSilent({
account: accounts[0],
scopes: (scopes ? scopes : [...msalScopes, ...graphScopes]).filter(onlyUnique)
});
return idToken;
}
Reproduction Steps
- Implement the code above
- login
- get an id-token
- wait 1 hour
- get another id-token, which is expired
Expected Behavior
The id-token should be refreshed before it expires.
Identity Provider
Azure AD / MSA
Browsers Affected (Select all that apply)
Chrome, Firefox
Regression
No response
Source
External (Customer)
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 22
- Comments: 38 (6 by maintainers)
Commits related to this issue
- fix: renew JWT token to avoid its expiration - EXO-65633 (#74) Every one hour, the MS Office connector fails to connect to outlook agenda to retrieve events. This is because the token expires and is ... — committed to exoplatform/agenda-connectors by ahamdi 3 years ago
- fix: renew JWT token to avoid its expiration - EXO-65633 (#74) Every one hour, the MS Office connector fails to connect to outlook agenda to retrieve events. This is because the token expires and is ... — committed to exoplatform/agenda-connectors by ahamdi 3 years ago
- fix: renew JWT token to avoid its expiration - EXO-65633 (#74) Every one hour, the MS Office connector fails to connect to outlook agenda to retrieve events. This is because the token expires and is ... — committed to exoplatform/agenda-connectors by ahamdi 3 years ago
- fix: renew JWT token to avoid its expiration - EXO-65633 (#76) Every one hour, the MS Office connector fails to connect to outlook agenda to retrieve events. This is because the token expires and is ... — committed to exoplatform/agenda-connectors by ahamdi 3 years ago
- fix: renew JWT token to avoid its expiration - EXO-65633 (#75) Every one hour, the MS Office connector fails to connect to outlook agenda to retrieve events. This is because the token expires and is ... — committed to exoplatform/agenda-connectors by ahamdi 3 years ago
I am seeing the same issue in my apps. I chose a variation on the workaround to dynamically set the
forceRefreshoption based on the id_token being expired. If you have LOTS of API calls using the id_token leveraging the cache is very important…Looking forward to fix in MSAL library.
Hi, we are facing this exact same issue. The workaround by forcing a refresh is suboptimal for various reasons :-
Is there any eta for an update for this?
According to MS documentation
So if I want the ID token (which always expires after 1 hour) to always be renewed at most 5 minutes before its expiration, I can just set
tokenRenewalOffsetSecondsto 35 minutes:Version:
"@azure/msal-browser": "^3.15.0"Just adding to the above that I’m also running into the ID token refresh issue when configured for NAA using
createNestablePublicClientApplicationwith the following config:However, the current workaround to use forceRefesh doesn’t appear to resolve the issue, and having a quick look through the source code, I can’t seem to see if NAA supports forceRefresh?
I’m going to drop a link on ID Tokens as a reference if folks have additional questions on ID Token usage.
https://learn.microsoft.com/en-us/azure/active-directory/develop/id-tokens
For this particular issue we’re going to have to make an API/interface change to address this to request explicit token types. For instance, you should be able to just request an Access Token without a Refresh Token, or just an ID Token. We’ve got an internal item tracking for this work for a future major release. In the interim, this won’t be changed for MSAL.js v2.x.y releases.
What happens if you provide
forceRefresh: trueon theacquireTokenSilentrequest object?I experience the exact same issue. Could you please have a look on this one @tnorling?
This is still an issue with the latest versions in 2025: “@azure/msal-browser”: “^4.0.1”, “@azure/msal-react”: “^3.0.1”,
instance.acquireTokenSilent will return me an expired id token. (If the non-aligned expiry of the access token has not yet expired, since those seem to range from 60-90 minutes, whereas the id token expires in 60 minutes.)
I’m using an ID token because the Access tokens provided by Microsoft do not have a valid signature, (jwt.verify, etc. will return “invalid signature”), while the ID token has a valid signature. (That’s a separate bizarre issue that has caused many developers frustration.)
I’ve tried using:
tokenRenewalOffsetSeconds: 35 * 60,but that seems to make no difference on the dates for the tokens received.It seems the only reasonable alternative is to use: forceRefresh: true Although that defeats the whole concept of caching, etc.
Using a library and an authentication service like Microsoft is supposed to be easier than building something from scratch… why are we having to deal with issues that 4yrs later are still a problem?
Hi Everyone,
We have the same challenge. We use the id token to call a vendor API. But it expires around 65 minutes. What we do has a few steps.
I did notice that MSAL library check only access token’s lifetime which expires around 86 minutes, having a longer life time than id token which has around 65 minutes. the 21 minutes in between is causing a 403 error when we are trying to call the vendor API.
As tnorling stated on 11/1/2021:
I suspect this is happening when the access token has a longer validity than the id token, if that’s the case we’ll need to re-evaluate how we do cache lookups today. We currently only check the access token validity because we made a naive assumption that the cached idToken would always have at least as much time left as the access token.
If MSAL could check both id token and access token, or whichever has shorter lifetime, then get a new set of tokens, which will solve this issue. Hope this can be worked on in the near future.
Thanks!
The initial assumption we made when we designed the library is the uniformity between expiry times for id_token and access_token. However, this is not true in all cases and we need to enhance the library to support these various expiry times. Added this to our internal backlog.
However, please note that this would need some redesign from our end and hence will take some time.
I see the same issue with idTokens not being refreshed and I’m running @angular/msal-browser 2.21.
While I appreciate that access tokens are recommended, in my service, the backend validates the idToken w/ .NET Core middleware and then uses a custom Role based system. The design is like this as pre-2.x versions of MSAL worked without issue to retrieve idTokens. No extra scopes/consent were required and we could simply pass ClientId to get the idToken. Additional scopes/resource access, since we use a custom Role-based system, are not needed. Also, since I’m using “First Party” AAD (4-5 instances), it becomes painful to add custom scopes since there are encryption requirements when additional scopes are added. It’s a fair number of hoops to jump through to make something work, tbh.
At any rate, it has become somewhat problematic in that the idToken will be expired, won’t be refreshed, and then 401 errors are returned on the API calls. It seems the only work-around for the users has been to force a logout in the AAD tenant. I may try “forceRefresh” in conjunction with checking the expiry, but this does potentially cause quite a bit more traffic back to the AAD tenant. On a related note, it would be nice if it were possible to tell MsalInterceptor to use the idToken instead of accessToken attaching to auth headers.
@its-miller-time
Access tokens are scoped to a single resource. If you need to access MS Graph APIs in addition to your own API you need 2 access tokens. However, this does not mean you need more than 1 app registration, you can register as many API permissions as you need on a single app registration.
The issue here is that AAD changed access tokens lifetimes from a static 60 minutes to a variable 60-90 minutes and MSAL.js only checks access token lifetimes. Id tokens still have a static 60 minute lifetime.