Skip to content

SQL Token Cache Provider Has Trouble Handling Different Client Applications Using the Same User Token Cache Record #158

Closed
@Alfetta159

Description

@Alfetta159

Please provide us with the following information:

This issue is for a: (mark with an x)

- [X] bug report -> please search issues before submitting
- [ ] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

The issue was found for the following scenario:

Please add an 'x' for the scenario(s) where you found an issue

  1. Web app that signs in users
    1. with a work and school account in your organization: 1-WebApp-OIDC/1-1-MyOrg
    2. with any work and school account: /1-WebApp-OIDC/1-2-AnyOrg
    3. with any work or school account or Microsoft personal account: 1-WebApp-OIDC/1-3-AnyOrgOrPersonal
    4. with users in National or sovereign clouds 1-WebApp-OIDC/1-4-Sovereign
    5. with B2C users 1-WebApp-OIDC/1-5-B2C
  2. Web app that calls Microsoft Graph
    1. Calling graph with the Microsoft Graph SDK: 2-WebApp-graph-user/2-1-Call-MSGraph
    2. With specific token caches: 2-WebApp-graph-user/2-2-TokenCache
    3. Calling Microsoft Graph in national clouds: 2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph
  3. Web app calling several APIs 3-WebApp-multi-APIs
  4. Web app calling your own Web API
  5. Web app restricting users
    1. by Roles: 5-WebApp-AuthZ/5-1-Roles
    2. by Groups: 5-WebApp-AuthZ/5-2-Groups
  6. Deployment to Azure
  7. Other (please describe)

Repro-ing the issue

Repro steps

Create facsimile apps but with more than one web client application that uses the same SQL user token cache table (i.e. more than one Azure registered client app ID.)

Run one client application and log on with a particular work account, then run the other client application and log on with that same account.

Expected behavior

Both apps should allow me to log on via that single signed on account.

Actual behavior

The second application to try logging on throws a encryption exception at

args.TokenCache.DeserializeMsalV3(tokenCacheBytes, shouldClearExistingCache: true);

MSALAppSqlTokenCacheProvider.cs, line ~184

Possible Solution

In ClaimPrincipalExtension.cs, add the application ID to the Msal Account ID to differentiate which application wrote to the UserTokenCache table.

public static string GetMsalAccountId(this ClaimsPrincipal claimsPrincipal)
{
	string userObjectId = GetObjectId(claimsPrincipal);
	string tenantId = GetTenantId(claimsPrincipal);
			
	// Same as GetObjectID, but look for claimtype == "aud"
	var applicationId = GetApplicationId(claimsPrincipal);
	if (!string.IsNullOrWhiteSpace(userObjectId) && !string.IsNullOrWhiteSpace(tenantId))
	{
		return $"{userObjectId}.{applicationId}.{tenantId}";
	}

	return null;
}

Update:

My suggested solution is not very good in that the aud claim isn't present during logging out. I'm working around this by adding the claim to the User during the OnRedirectToIdentityProviderForSignOut event just before calling the RemoveAccount method. I'm curious to see if the problem lies elsewhere first.
Additional context/ Error codes / Screenshots

Any log messages given by the failure

Add any other context about the problem here, such as logs.

(N/A)

OS and Version?

Window 10, VS 2019, .NET Core 2.2

Versions

ASP.NET Core 2.2

Attempting to troubleshooting yourself:

Mention any other details that might be useful


Thanks! We'll be in touch soon.

Metadata

Metadata

Assignees

Labels

AnsweredquestionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions