Following the guidelines highlighted at https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-acquire-cache-tokens , every time before I make an API call I'll first call acquireTokenSilentAsync to get the access token either from cache, or a new one automatically using the refresh token.
This works well, except that it seems to be quite slow in retrieving from cache. See below screenshot for timing:
As you can see when I just started the app, it manage to retrieve the access token used in the last run (I copied it out to a text file and compared) it took 2 seconds. Subsequently it became faster on each run, but it still take more than half a second to retrieve the exact same access token from cache. Loading from cache should take just about 5ms to 10ms?
This is my acquireTokenSilently implementation, just basic configuration.
private fun acquireTokenSilently(serviceName: String, callback: IAcquireTokenSilentlyCallback) {
val parameters: AcquireTokenSilentParameters = AcquireTokenSilentParameters.Builder()
.withScopes(authConfigReader.getServiceScopes(serviceName))
.forAccount(firstAccount)
.fromAuthority(authConfigReader.authorityUrl)
.withCallback(object : SilentAuthenticationCallback {
override fun onSuccess(authenticationResult: IAuthenticationResult) {
callback.onSuccess(authenticationResult.authorizationHeader)
}
override fun onError(exception: MsalException) {
callback.onError(mapMsalException(exception))
}
})
.build()
publicClientApp!!.acquireTokenSilentAsync(parameters)
}
Do we need to write our own token persistence implementation like the one mentioned in the MSAL for Java section? This is not mentioned in the MSAL for Android section though: https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-java-token-cache-serialization
Thanks!
Bruce