1

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.

dmc
  • 807
  • 2
  • 10
  • 25
  • How do you test the upgrade scenario on Android? – Julian Jun 06 '23 at 11:39
  • @Julian I've tried it on both debug mode via visual studio by switching between projects and deploying to the device, and also have tried it via Google Play's Internal Testing with the signed apk (e.g. install our xamarin forms version which sets a value in secure storage, then upgrade to the maui version of our app which is supposed to get the value from secure storage). I'm getting the same results in both scenarios/tests. – dmc Jun 06 '23 at 11:44
  • And you're using the same app id and the same signing keys? – Julian Jun 06 '23 at 11:55
  • @Julian yes that is correct. Google Play's Internal Testing even says "Update" when going to our maui versions internal download url instead of "Install". Not sure if it's worth mentioning that I am testing on Android 13 device. – dmc Jun 06 '23 at 12:01
  • Do you have the "Preserve application data cache on device between deploys" option enabled in the *Xamarin -> Android Settings* in Visual Studio 2022? – Julian Jun 06 '23 at 12:42
  • Hi @Julian yes it is currently checked/enabled at the moment. – dmc Jun 06 '23 at 12:47

1 Answers1

0

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.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14