How to share user token between two different applications ?

Copper Contributor

Hi everyone

 

I would like to achieve this:

I have two applications using adb2c for authentication. I would like a user to be able to sign in interactively in the first application and then be logged in silently in the second application.

 

What I have for now:

Both applications are registered in Azure environment, with two different client id.

Both applications share the exact same code, same cache and same configuration, except for the client id.

 

My issue is:

If I use the same client id for both applications (as if they were registered as a single application), the user can sign interactively in one application and then be logged in silently in the second one.

But, if I use two different client id, it is no longer working. If the user first signed in interactively in application 1, when I call GetAccountsAsync() in application 2 to get the user account in cache, it returns no account.

 

My code:

To acquire token interactively:

private async Task<AuthenticationResult> AcquireTokenInteractiveAsync()
{
   try
   {
      AuthenticationResult ar = await pubClientApp.AcquireTokenInteractive(config.ApiScopes)               .WithB2CAuthority(config.GetAuthoritySignUpSignIn()).WithExtraQueryParameters(ui_locale).ExecuteAsync();
      return ar;
   }
   catch (MsalException ex)
   {
   }
   catch (Exception ex)
   {
   }
   return null;
}

 

To acquire token silently:

private async Task<AuthenticationResult> AcquireTokenSilentAsync()
{
   try
   {
      var accounts = await pubClientApp.GetAccountsAsync();
      if (accounts.Count() > 0)
      {
         AuthenticationResult ar = await pubClientApp.AcquireTokenSilent(config.ApiScopes,accounts.FirstOrDefault()).ExecuteAsync();
         return ar;
      }
   }
   catch (Exception ex)
   {
   }
   return null;
}

 

Thanks for your help :)

1 Reply
Hi
Does the applications are leveraging Azure resources .
If yes they can leverage Service Principal / Manage Identities
If not i would suggest to have a look on Azure Identity library
There is also an interesting article here : https://www.rahulpnath.com/blog/defaultazurecredential-from-azure-sdk/