Search code examples
androidxamarinactive-directoryxamarin.androidadal

Does client need to persist and refresh token itself when using Azure Active Directory Library?


In our Xamarin.Android app, we are using ADAL for .Net v3.13.5 to get the access token.

var authContext = new AuthenticationContext(AD_AUTHORITY);

if (authContext.TokenCache.ReadItems().Count() > 0)
{
    authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
}

var authResult = await authContext.AcquireTokenAsync(API_RESOURCE_ID_URI, CLIENT_ID, new Uri(REDIRECT_URI), new PlatformParameters(callerActivity));
return authResult.AccessToken;

It seems the token is stored by ADAL only in memory. Every time the app restarts, the user has to login again.

Is there a way to make ADAL persist the token, so that the token is available even if app restarts, or the app itself needs to take care of token persistence and refresh logic?


Solution

  • I believe there is a bug serialization that results in the issue you are seeing. This means you would have to take ownership of the TokenCache yourself when constructing the AuthorizationContext.

    https://forums.xamarin.com/discussion/58736/crash-in-release-build-when-using-datacontractjsonserializer