Fix Error 18456 with Entra ID auth when re-authenticating with stale tokens (#18456)#22063
Fix Error 18456 with Entra ID auth when re-authenticating with stale tokens (#18456)#22063ahernandez-developer wants to merge 2 commits into
Conversation
|
@microsoft-github-policy-service agree |
Codecov Report❌ Patch coverage is ❌ Your patch status has failed because the patch coverage (20.00%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #22063 +/- ##
==========================================
- Coverage 74.95% 74.62% -0.33%
==========================================
Files 393 344 -49
Lines 119869 106319 -13550
Branches 7161 6610 -551
==========================================
- Hits 89847 79343 -10504
+ Misses 30022 26976 -3046
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
I see two CI items, but not sure if releated, first contribution to this repo, seems unrelated to the code changes:
Should I add tests? |
|
@ahernandez-developer no need to add tests to those, but I'd appreciate your help in reproducing the issue so that I can confirm your fix.
and, of course, the big one:
Thanks again for your contribution, @ahernandez-developer! Additional details: Currently (on 1.42.2), I'm able to connect with a database in Tenant A with an account in Tenant B with Tenant A selected as tenant acquire a token via:
To give the account access, I ran: -- when connected to the server in tenant A
CREATE USER [account@tenantB.com] FROM EXTERNAL PROVIDER;
ALTER ROLE db_owner ADD MEMBER [account@tenantB.com]; |

Summary
Fix Error 18456 with Entra ID auth when re-authenticating with stale tokens
detect AADSTS50078 (wrong audience) and re-authenticate with correct scopes
compute resource-specific scopes for SQL endpoints instead of reusing ARM-only tokens
fix token expiry derivation from tokenResult.expiresOn instead of idTokenClaims
guard null access on refreshed account in MsalAzureController
Fixes #22031
Reproduction
The issue triggers when a stale refresh token forces re-authentication:
Confirmed on both multi-tenant (account in Tenant A, DB in Tenant B) and single-tenant setups
Validation
Connection succeeds — no Error 18456 on first connect or reconnect
extensions/mssql/src/azure/msal/msalAzureDeviceCode.ts extensions/mssql/src/azure/msal/msalAzureController.ts extensions/mssql/src/controllers/connectionManager.ts
Before fix: Error 18456 on connect.
After (fix):
Authority URL set to: https://login.microsoftonline.com/organizations
Failed to acquireTokenSilent InteractionRequiredAuthError: AADSTS50173 (stale grant)
→ Interactive auth triggered with resource-specific scopes
Authority URL set to: https://login.microsoftonline.com/
Failed to acquireTokenSilent InteractionRequiredAuthError: AADSTS50173 (wrong tenant token)
→ Interactive auth with correct tenant authority
→ Connection established successfully