Reputation: 11
I am updating my app from Xamarin forms to flutter and trying to access both Shared Preferences and Secure storage for values that were stored in the Xamarin app.
I used Xamarin Essentials for both Preferences and secure storage and am trying to access the same information in Flutter using the SharedPreferences and FlutterSecureStorage plugins, but cannot seem to access the old preferences after updating the app (both android and iOS). Any help on this would be appreciated.
Code in old Xamarin Forms app
string secureValue = await SecureStorage.GetAsync("secure_value");
string preferenceValue = SettingsHandler.GetSetting("preference_value");
Code in updated Flutter app:
static final _storage = FlutterSecureStorage();
String secureValue = await _storage.read(key: "secret_value");
SharedPreferences prefs = await SharedPreferences.getInstance();
String preferenceValue = await (prefs.get("preference_value"));
Upvotes: 1
Views: 329
Reputation: 767
So I managed to get it to work for iOS.
The SecRecord used to store the value has a Service value set to [YOUR-APP-BUNDLE-ID].xamarinessentials.
flutter_secure_storage
you can pass in the iOS option final iosOptions = IOSOptions(accountName: '${environmentVariables.iosAppId}.xamarinessentials');
final value = await secureStorage.read(key: key, iOptions: iosOptions);
When I update my iOS app from the Xamarin version to the Flutter version I can now get same token from the secure storage.
For Android
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.
flutter_secure_storage
you can pass in the Android option final androidOptions = AndroidOptions(sharedPreferencesName:'${environmentVariables.androidAppId}.xamarinessentials');
final value = await secureStorage.read(key: key, aOptions: androidOptions);
I am not yet able to confirm if this works for Android and will have to do some more testing.
Upvotes: 2
Reputation: 791
I'll be amazed if it's possible to obtain the same data again that was stored with xamerin with flutter.
Take Android, Flutter and Xamerin both use the Android KeyStore behind the scenes but they are encrypted with different keys and stored as a different file:
e.g. xamerin docs:
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.
Flutter secure_storage
won't use the .xamerinessentials file. Also, since v5.0.0 it doesn't use Android KeyStore it actually now uses EncryptedSharedPreferences
Upvotes: 1