Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] AADSTS50020 error if signing out then into another tenant without restarting the app #1365

Closed
1 task done
osolo opened this issue Sep 2, 2019 · 10 comments
Closed
1 task done
Assignees
Labels
Milestone

Comments

@osolo
Copy link

osolo commented Sep 2, 2019

Which Version of MSAL are you using ?
MSAL 4.3.1

Platform
all

What authentication flow has the issue?

  • Desktop / Mobile
    • Interactive

Description

In UWP apps (possibly also in non-UWP), if you execute the following sequence, an AADSTS50020 is issued:

  1. AcquireTokenInteractive() [user logs in successfully to CompanyA.com]
  2. AcquireTokenSilent() [this is required for the bug to appear]
  3. Log out (see code below)
  4. AcquireTokenInteractive() [user tries to login into CompanyB.com]

The user should be able to log into CompanyB, however we get this error:

AADSTS50020: User account '[someemail]' from identity provider 'https://sts.windows.net/[some_guid]/' does not exist in tenant '[some name]' and cannot access the application '[some_guid]'(AppName) in that tenant. The account needs to be added as an external user in the tenant first. Sign out and sign in again with a different Azure Active Directory user account.

Logout looks like this:

var accounts = await _graphApp.GetAccountsAsync();
while (accounts.Any())
{
	await _graphApp.RemoveAsync(accounts.FirstOrDefault());
	accounts = await _graphApp.GetAccountsAsync();
}

Additional Information

  1. If you restart the app between steps 3 and 4, things work as expected (user can log into CompanyB).
  2. Adding .WithAuthority(AadAuthorityAudience.AzureAdMultipleOrgs) to the app builder does not resolve the problem.

I have provided a sample app to demonstrate this problem. You must supply your own AppId.

@bgavrilMS
Copy link
Member

I do not understand the repro steps, please clarify. You have a user e.g. john@companyA.com that is member of 2 tenants - companyA and companyB (home tenant companyA and guest tenant companyB).

How does the user login to "companyA" and to "companyB"?

@bgavrilMS bgavrilMS self-assigned this Sep 4, 2019
@osolo
Copy link
Author

osolo commented Sep 4, 2019

Separate users in separate tenants.

For example, using the provided app, log in with your work (@microsoft.com) account. Then log out and try to log in using any private/home Microsoft Account or an account on any other tenant (such as a demo tenant).

@bgavrilMS
Copy link
Member

Can repro.

@bgavrilMS bgavrilMS added the bug label Sep 4, 2019
@bgavrilMS
Copy link
Member

Can repro on other platforms, not just UWP.

@bgavrilMS
Copy link
Member

Workaround: re-create the public client application before each request i.e.

var pca = PublicClientApplicationBuilder .Create(AppId) .Build();

This is a bug where we update the authority with the tenant ID in AcquireTokenSilent. The same authority (tenanted to companyA) is then re-used to issue a request for a token for a user in companyB.

@osolo
Copy link
Author

osolo commented Sep 4, 2019

I can confirm that the workaround indeed works :)

@adamedx
Copy link

adamedx commented Sep 6, 2019

FYI, I hit this same problem, looking forward to a release with the fix and will share results once I get to try it out. Glad this issue was filed as I had been spending time trying to debug -- I assumed it was my fault as I was in the process of migrating from the older MSAL with explicit caches to the new builder model with implicit cache.

@henrik-me henrik-me added this to the 4.3.2 milestone Sep 6, 2019
bgavrilMS added a commit that referenced this issue Sep 9, 2019
@bgavrilMS bgavrilMS changed the title [Bug] AADSTS50020 error if signing out then into another tenant without restarting the app (UWP) [Bug] AADSTS50020 error if signing out then into another tenant without restarting the app Sep 9, 2019
@henrik-me
Copy link
Contributor

henrik-me commented Sep 21, 2019

@osolo and @adamedx Included in the 4.4.0 release

@adamedx
Copy link

adamedx commented Oct 6, 2019

Yes, I updated shortly after it was released and everything works now :). I was able to incorporate it into a release (was originally planning on just relnoting the bug). Thanks very much for the quick turnaround on diagnosis, fix, and release.

@jmprieur
Copy link
Contributor

jmprieur commented Oct 6, 2019

Thanks for the update @adamedx. Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants