dmc
dmc

Reputation: 817

SecureStorage.GetAsync in MAUI app returns null when trying to get the kev/value that was Set in Xamarin Forms app

We have just migrated our Xamarin Forms app to MAUI and we are going through some testing to make sure the upgrade process will be hassle free for our users once we release the new MAUI build in stores.

Our Xamarin Forms app maintains some values in SecureStorage (e.g. SetAsync and GetAsync). Now since we've migrated to MAUI, our users will be coming from the Xamarin Forms app and upgrading to the MAUI app. And as we are testing this process, we have noticed that GetAsync always comes back null in our MAUI app when trying to retrieve the value that the Xamarin Forms app Set. The Xamarin Forms and MAUI app have the same App Id, so we assume there shouldn't be any issues on this.

What are we missing?

Additional Info: In iOS, this worked fine when deploying the Xamarin Forms app to our test device, then deploying the MAUI app to overwrite the app. However, this didn't work in our TestFlight build, so we ended up using NSUserDefaults.StandardUserDefaults.SetString and NSUserDefaults.StandardUserDefaults.StringForKey for iOS which works. However we want to get the SecureStorage working for Android, and if possible for iOS if we are missing something.

Upvotes: 2

Views: 1360

Answers (1)

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14509

The official document about the SecureStorage in the xamarin said:

The Android KeyStore is used to store the cipher key used to encrypt the value before it is saved into a Shared Preferences with a filename of [YOUR-APP-PACKAGE-ID].xamarinessentials. The key (not a cryptographic key, the key to the value) used in the shared preferences file is a MD5 Hash of the key passed into the SecureStorage APIs.

And the official document about the SecureStorage in the maui said:

SecureStorage uses the Preferences API and follows the same data persistence outlined in the Preferences documentation, with a filename of [YOUR-APP-PACKAGE-ID].microsoft.maui.essentials.preferences. However, data is encrypted with the Android EncryptedSharedPreferences class, from the Android Security library, which wraps the SharedPreferences class and automatically encrypts keys and values using a two-scheme approach:

Keys are deterministically encrypted, so that the key can be encrypted and properly looked up. Values are non-deterministically encrypted using AES-256 GCM. For more information about the Android Security library, see Work with data more securely on developer.android.com.

First of all, the value stored in the different files : one is xamarinessentials and the other is microsoft.maui.essentials.preferences. In addition, the maui used a new method to encrypt the key which is not same as the Xamarin. So that's why can't get the old value in the storage when you update the xamarin to maui.

You can try to use the code in my old answer about getting the value in the securestorage to get the old value and then store it in the maui manuall. Or you can also post an new issue on the github.

Upvotes: 0

Related Questions