-
Notifications
You must be signed in to change notification settings - Fork 64
MSAL B2C token refresh flow - multiple APIs #139
Description
Hello team!
This is a great sample, easy to understand and works well obtaining a token from B2C and calling an API.
There is a known limitation with B2C in the sense that the token refresh flow does not allow obtain tokens for a second, different API.
Doing so yields an id token, refresh token and a null access token!!
To obtain a second access token, AcquireTokenInteractive will need to be called; which may require a users to enter credentials (not an ideal user experience)
Additionally, this means, calling AcquireTokenSilent() and sending a second set of scopes runs … but does not returned an access token, nor does it generate any errors, or throw exceptions.
The error is surfaced later when a call to an API is make, failing with an invalidTokenError.
In my attempt to use MSAL to workaround this, I modified AcquireTokenSilent by adding a scope parameter; I also checked to see if the access token was null; if it was, I then called AcquireTokenInteractive.
public async Task<UserContext> AcquireTokenSilent(string[] scopes)
{
IEnumerable<IAccount> accounts = await _pca.GetAccountsAsync();
AuthenticationResult authResult = await _pca.AcquireTokenSilent(scopes, GetAccountByPolicy(accounts, B2CConstants.PolicySignUpSignIn))
.WithB2CAuthority(B2CConstants.AuthoritySignInSignUp)
.ExecuteAsync();
if (authResult.AccessToken == null)
{
//acquire token interactive ...
authResult = await _pca.AcquireTokenInteractive(scopes)
.WithPrompt(Prompt.NoPrompt)
.WithAuthority(B2CConstants.AuthoritySignInSignUp)
.ExecuteAsync();
}
var newContext = UpdateUserInfo(authResult);
return newContext;
}
This “gets the job done” in the sense that a second access token is obtained, but briefly pops up a blank screen.
The ask:
Can this sample be modified to take into account this B2C limitation? Either:
- Throw an exception when a null token is returned
- Once detected, could a different request be made, such as AcquireTokenInteractive?
- Can anything be done to make this seamless, so that there are no prompts?
Thank you