MSGraphClient token not refreshed properly - all calls will fail after expiration
Category
- [x] Bug
Describe the bug
The bearer token for Graph doesn't get updated properly and will not refresh once expired, causing all calls done via the MSGraphClient to fail.
We have a single app page running on SPFx 1.10 which does occasional calls to graph endpoints using the MSGraphClient. Starting last week Wednesday these calls fail intermittently after 1 hour (when machine goes into sleep mode, network is offline, etc). We can see that the token does not get refreshed, but the expiration value DOES change when it attempts to get a new token. Removing both values from the SessionStorage helps most of the time, the GraphClient will then get a new token and things work again (very rarely we will get the old token again, which is expired???).
I've created a small sample webpart which does a call to /me every 5 minutes and prints results for the call & token values. Here we see the value for the expiration increase every call, obviously the token itself stays the same. It won't update the token and when it expires we see the call starting to fail.

Upgrading to 1.11 doesn't seem to fix the bug, and due to QA requirements we'd like to keep to 1.10 if at all possible.
Steps to reproduce
Run a graph call on the page once every N time and wait for the token to expire. If this fails, see how you won't get another token unless you remove the value from the SessionStorage. You may need to disable network, or put the machine in sleepmode for easier reproduction. It doesn't happen consistently, if you have the browser open in the background it tends to work ok most of the time.
Expected behavior
I expect the token to properly refresh once it expired.
Environment details (development & target environment)
- Your Developer Environment: Windows 10 - also happens on MacOS
- Target Environment: SharePoint Online
- Framework: SPFx 1.10 / SPFx 1.11
- Browser(s): Chrome, Safari, Edge, Chromium Edge, IE11
Additional context
I can provide a sample project, and if wanted we can demo this within our product.
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
MSFT is in the midst of rolling out a fix related to obtaining a token (#4892)... not sure if it's related to this. @lahuey, do you know if this is part of that fix?
We have the similar issue with AadTokenProvider. As soon as token is expired it is not being renewed and app is stuck until session storage is cleared (thanks for the tip)
We are having the exact same issue with AadHttpClient in our SPFx web parts that consume Azure AD-Secured APIs. The first time user accesses a page with an SPFx web part that consumes an Azure AD-Secured API it works fine. But after a period of inactivity like 30 mins, if the user tries to access the same page or another page with another SPFx web part that makes an Azure AD-Secured API call the request fails with a 401 unauthorized error. Based on our troubleshooting, the AadHttpClient is still using an expired access token from a previous session. The older access token gets cached and there is no way to get out of this. Closing the browser or clearing browser cache doesn't resolve this issue. This is the same behavior in new Edge and Chrome.
SharePoint Online REST APIs are working fine. We have a few pages that have multiple web parts and the web parts consuming SPO APIs are working fine but the ones consuming Azure AD-Secured APIs are failing.
We are currently experiencing production outages due to this issue. A critical case was opened with Microsoft yesterday but seems like we are having very hard time trying to get to the product team. This is absolutely frustrating as our hands are totally tied and we are not getting much help from Microsoft.
Are there any updates on this issue?
We're also seeing this exact issue on our solutions. Refresh token isn't refreshed properly. Kill & re-open the tab (so the session storage is cleared) and it comes back to life.
We've been working with support for a while on this issue, and it should have landed at the Product Group now. No solution in sight as of yet though :(
I have found that the issue is that the expiration value in the browser session cache is out of sink with the expiration on the token itself. So the token in the session cache is expired, but the expiration value shows it is still valid. This is under review from the product group.
Any news about this Bug ? I have the same error the MSGraphClient token not refreshed properly,
We are also experiencing this problem in an application. The application relies on the aadHttpClient to retrieve information from a back-end API, and calls stop working after one hour.
By inspecting the session storage at the expiration time, we can see that the expiration time (key "...|adal.expiration.key|...") is updated, but the JWT token (key "...|adal.access.token.key|...") is not. By inspecting the HTTP traffic with Fiddler, I could not find a connection to login.microsoftonline.com, so I suppose no new token was retrieved.
Checking with SP engineering... not sure if this is a bug (ie: SPFx API should automatically fetch a new access token after expiry) or by design (ie: something the developer would have to manage). Once I know something I'll follow up...
@andrewconnell I don't think the token provider used by MSGraphClient is even publically available via the API... (I could be wrong)
@jimmywim Pretty sure it is... this isn't a complete sample, but you get the picture. None of those APIs or methods are marked as internal in the type declarations which = public.
this.context.aadTokenProviderFactory.getTokenProvider().then((p) => {p.getToken})
Ah, and is the TP returned by that factory the same as is used in MSGraphClient? i.e. preconfigured with the Client ID of the app registration in Azure, etc.? If so you may be onto something (but we'll wait til engineering say what's the way forward).
The only thing the current one does is (1) use the AadHttpClient to obtain an access token (because MSGraph is an Azure AD secured resource) & (2) initialize the MS Graph JS SDK.
IOW, the MSGraphClient in the SPFx API is just a thin helper wrapper to existing APIs. That's why the original Graph client was deprecated & retired... it was a full SDK built by the SP team to talk to the Graph REST API, which makes no sense for them to maintain after the Graph team released a JS SDK.
Got it - makes sense now, thanks - also now understand why token refresh may be something that we need to look after if the TP isn't doing it automagically for us.
That's the part I'm not sure about... is it intended for the SPFx API to do this for you, or not. I can see both sides... that's why I'm checking with engineering if this is by design or if something isn't working.
Same issue here in production solutions that are calling AAD protected API endpoints... never noticed this issue before though, while I'm using the AadHttpClient a lot. Not sure how this one sneaked in.
Spoke to SP engineering... here's the gist of it:
They are aware of this and a few other identity things. You may have noticed that MSFT recently announced they were dropping support for ADAL.JS which is what the SPFx API leverages for token ACK.
They are working to update their identity libraries to MSAL.js(IMHO, the right call) which they believe will resolve some of these known issues... including the one in this issue (as well as others... for example, some have creeped up due to recent changes in Chrome & Safari).
Before you ask... do I have an ETA? Nope... they are working on it. My $0.02: I imagine there are complexities (it is identity stuff) so it's more important this is done right than fast.
So... I guess all I can say: stay tuned... :-/
The new implementation has been deployed to one of our tenants and indeed solved the problem. We are now waiting for the other 2 tenants to be updated too.
@lucmoco can you please provide more details on new implementation. We are also facing this issue in our prod site and we need to resolve it on ASAP basis. It would be great if you could provide the changes we need to make our tenant work as well.