maui.net-8.0maui-blazor

.NET MAUI Secure storage values return null after app is closed or phone is asleep for 10 seconds


As the title states, I have a .NET MAUI app that I am currently running on an iOS device. The app saves a token inside of secure storage like this:

await SecureStorage.Default.SetAsync("accounttoken", "token here");

I then use this token to authorize with an API used by the app, however, when I fully close the app or wait 10 seconds after my phone sleeps, all values stored in secure storage seem to get wiped. I know this because, when the following line hits, it returns null:

await SecureStorage.Default.GetAsync("accounttoken");

I am not removing the token at any point, and have removed all custom lifecycle methods. I even tested by adding dummy values to secure storage and those get wiped too. Any ideas as to what might be happening? As I understand it, values in secure storage should persist indefinitely.


Solution

  • I discovered that as soon as the phone is unlocked, I am able to retrieve the values again (they are no longer null). My assumption is that Secure Storage can't be accessed while the phone is locked or in the background. I imagine this is intended behavior.