I have a curiously curious bug happening in my app.
I have this very simple helper method I use to store some cool data on the user device:
public static async Task TrySetAsync(string key, string value)
{
try
{
await SecureStorage.SetAsync(key, value);
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert(
"Device incompatible",
"Your device is not compatible with this app.",
"Ok");
}
}
It works perfectly fine and never had a problem, but I decided to perform a compatibility check on the app start so the user knows immediately their device won't cut it. To do that, I decided to implement it in the OnAppearing() method of the initial login page. It looks like this:
protected override async void OnAppearing()
{
try
{
// Try to save data to a random container
string tempKey = System.Guid.NewGuid().ToString();
// Should throw an exception if SecureStorage is not compatible
await Xamarin.Essentials.SecureStorage
.SetAsync(tempKey, string.Empty);
// Remove the datum
Xamarin.Essentials.SecureStorage
.Remove(tempKey);
}
catch
{
await App.Current.MainPage.DisplayAlert(
"Device incompatible",
"This device does not support secure storage required for the application.",
"Ok");
App.Terminate();
}
base.OnAppearing();
}
I removed the Entitlements from the project to test my little concoction, and yet, it passes through it without a problem. No exception gets thrown. But upon logging in, where the original TrySetAsync method is used (first code snippet), the expected exception is thrown (missing entitlement). The debugger enters the try block and leaves it without an issue.
In summary:
It appears, that the app does not require any entitlements when SecureStorage is referenced in the OnAppearing(), yet throws the expected exception in TrySetAsync(...) defined in the main App class.
Any advice?
P.S. Originally I used GetAsync with a random guid string in OnAppearing(), which I changed to SetAsync and Remove, thinking that maybe getting data does not require compatibility. In general I never had an issue like that with SecureStorage and it really seems like some internal bug to me.\
ANSWER
My bad for using string.Empty as a value. It appears that SecureStorage.SetAsync will ignore such value and not store it, which then results in no exception being thrown at all. Also, attempting to GetAsync a non-existent key from SecureStorage will also not result in a missing entitlement exception