I was able to replicate this almost exactly. Running under an IDE debugger, it works perfectly, but if it's not attached to a debugger, or running as a release version, it silently fails to store the token and everything else that comes after the attempted persistence usually fails to run as well.
I managed to resolve this same issue on Android by implementing my own cache derived from TokenCache
and looking at what it was trying to do.
I located an example for a desktop app, as described here:
https://learn.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-native-dotnet
and then had a look at their FileCache
as it was defined in their GitHub repo:
https://github.com/AzureADQuickStarts/B2C-NativeClient-DotNet/blob/complete/TaskClient/FileCache.cs
I copied their FileCache
, and added it to the PublicClientApplication
initialisation :
ClientApplication = new PublicClientApplication(SharedConstants.AuthContext, SharedConstants.ClientId)
{
RedirectUri = "urn:ietf:wg:oauth:2.0:oob",
UserTokenCache = new FileCache(),
};
(by default most examples leave the UserTokenCache
property empty, to presumably use the platform default shared prefs).
Then tweaked it to do some verbose logging during the reads/writes/accesses just to see what it was up to:
private void AfterAccessNotification(TokenCacheNotificationArgs args)
{
// if the access operation resulted in a cache update
try
{
this.Log().Debug("About to update token cache (if it's changed)...");
if (this.HasStateChanged)
{
this.Log().Debug("State has changed, updating cache file...");
lock (FileLock)
{
// reflect changes in the persistent store
_file.WriteAllBytes(CacheFilePath, this.Serialize());
// once the write operation took place, restore the HasStateChanged bit to false
this.HasStateChanged = false;
}
this.Log().Debug("Token cache file updated");
}
this.Log().Debug("Finished updating token cache file");
}
catch (Exception ex)
{
this.Log().ErrorException($"Something went wrong during token AfterAccessNotification: {ex.Message}", ex);
}
}
The logging showed that on Android, it was throwing an exception related to there not being a serialisation handler for a DateTimeOffset
-type property somewhere deep in the bowels of the MSAL library.
Once I wrapped the AfterAccessNotification
in a try-catch with a handler to log that the exception had occurred, it all started working perfectly.
I've stuck with using this version of FileCache
for now, because it mostly solves my problem for the moment, but it does mean that the auth tokens etc. aren't stored securely now.
I suspect the same problem occurs on iOS 10.x on the simulator as well, but I've not been able to verify this yet.
I hope this helps someone.
Edit: as @Henrik has mentioned in his comment: editing the project properties and telling it not to link the System.Runtime.Serialization.dll
on Android then fixes the problem permanently. Based on my (admittedly) limited testing, it's then safe to switch away from using the FileCache
to the default behaviour again.