-
Notifications
You must be signed in to change notification settings - Fork 343
AcquireTokenSilentAsync using a cached token
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 AcquireTokenSilentAsync
first, to verify if an acceptable token is in the cache, or can get derived. If not, a call to AcquireTokenAsync (in public client applications), or AcquireTokenXXX in confidential client applications will be needed. The only exceptions are:
-
AcquireTokenForClientAsync
(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 -
AcquireTokenByAuthorizationCodeAsync
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 AcquireTokenSilentAsync
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 AcquireTokenSilentAsync
, and if it fails with a MsalUiRequiredException
, call AcquireTokenAsync
AuthenticationResult result = null;
var accounts = await app.GetAccountsAsync();
try
{
result = await app.AcquireTokenSilentAsync(scopes, accounts.FirstOrDefault());
}
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.AcquireTokenAsync(scopes);
}
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
}
Previous versions of MSAL.NET were using IUser
instead of IAccount
. The code was as follows:
AuthenticationResult result = null;
try
{
result = await app.AcquireTokenSilentAsync(scopes, app.Users.FirstOrDefault());
}
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.AcquireTokenAsync(scopes);
}
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
AcquireTokenByAuthorizationCodeAsync
- 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