-
Notifications
You must be signed in to change notification settings - Fork 343
AcquireTokenSilentAsync using a cached token
This page is for MSAL 3.x. If you are interested in MSAL 2.x, please go to AcquireTokenSilentAsync using a cached token 2.x
Once MSAL.NET has acquired a user token to call a Web API, it caches it. Next time the application wants a token, it should first call AcquireTokenSilent
first, to verify if an acceptable token is in the cache, can be refreshed, or can get derived. If not, a call the AcquireTokenForFlow method depending on the flow you are interested in.
There are two flows before which you should not call AcquireTokenSilent
:
-
AcquireTokenForClient
(Client credentials flow), which does not use the user token cache, but an application token cache. This method takes care of verifying this application token cache before sending a request to the STS -
AcquireTokenByAuthorizationCode
in Web Apps, as it redeems a code that the application got by signing-in the user, and having them consent for more scopes. Since a code is passed as a parameter, and not an account, the method cannot look in the cache before redeeming the code, which requires, anyway, a call to the service.
Contrary to what happens in ADAL.NET, the design of MSAL.NET is such that AcquireTokenAsync
never looks at the cache. As an application developer, you need to call AcquireTokenSilent
first. AcquireTokenSilentAsync
is capable, in many cases, of silently getting another token with more scopes, based on a token in the cache. It's also capable of refreshing a token when it's getting close to expiration (as the token cache also contains a refresh token)
The recommended call pattern is to first try to call AcquireTokenSilent
, and if it fails with a MsalUiRequiredException
, call AcquireTokenXYZ
AuthenticationResult result = null;
var accounts = await app.GetAccountsAsync();
try
{
result = await app.AcquireTokenSilent(scopes)
.WithAccount(accounts.FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilentAsync.
// This indicates you need to call AcquireTokenAsync to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
try
{
result = await app.AcquireTokenInteractive(scopes)
.ExecuteAsync();
}
catch (MsalException msalex)
{
ResultText.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
}
}
catch (Exception ex)
{
ResultText.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
return;
}
if (result != null)
{
string accessToken = result.AccessToken;
// Use the token
}
For the code in context, see the active-directory-dotnet-desktop-msgraph-v2 sample
For Web applications that use OpenID Connect Authorization Code flow, the recommended pattern in the Controllers is to:
- instantiate a
ConfidentialClientApplication
with a token cache for which you would have customized the serialization See token cache serialization for Web apps - Call
AcquireTokenByAuthorizationCode
- Home
- Why use MSAL.NET
- Is MSAL.NET right for me
- Scenarios
- Register your app with AAD
- Client applications
- Acquiring tokens
- MSAL samples
- Known Issues
- AcquireTokenInteractive
- WAM - the Windows broker
- .NET Core
- Maui Docs
- Custom Browser
- Applying an AAD B2C policy
- Integrated Windows Authentication for domain or AAD joined machines
- Username / Password
- Device Code Flow for devices without a Web browser
- ADFS support
- Acquiring a token for the app
- Acquiring a token on behalf of a user in Web APIs
- Acquiring a token by authorization code in Web Apps
- High Availability
- Token cache serialization
- Logging
- Exceptions in MSAL
- Provide your own Httpclient and proxy
- Extensibility Points
- Clearing the cache
- Client Credentials Multi-Tenant guidance
- Performance perspectives
- Differences between ADAL.NET and MSAL.NET Apps
- PowerShell support
- Testing apps that use MSAL
- Experimental Features
- Proof of Possession (PoP) tokens
- Using in Azure functions
- Extract info from WWW-Authenticate headers
- SPA Authorization Code