maui: SecureStorage GetAsync not working in .NET 8 MAUI
Description
I am in the process of Migrating our mobile app from Xamarin to MAUI. I have found that SecureStorage.Default.GetAsync(ID_TOKEN); method not executing or returning any value in .NET 8. The same code is working for NET 7 but the issue happens only with the NET8(Preview) version.
I am using Visual Studio 2022 Version 17.8.0 Preview 2.0
This is a similar issue already reported on https://github.com/dotnet/maui/issues/12341.
Steps to Reproduce
- Create a sample MAUI application with .NET 8.
- Create two buttons, one for get and another for Set(SecureStorage.GetAsync and SecureStorage.SetAsync)
- Execute the MAUI app and click on the Setasync button(*this is randomly set an string value)
- Close the app
- Re-open the same app and click on GetAsync button.
- await getSecureStorage is kept waiting and the call is not executed
Link to public reproduction project repository
https://github.com/DharunAR/SecureTest/
Version with bug
8.0.0-preview.7.8842
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
7.0.49
Affected platforms
Android
Affected platform versions
Android 12 and Android 13
Did you find any workaround?
No.
Relevant log output
No
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 5
- Comments: 41 (6 by maintainers)
Using as below solved my problem:
var someSecureValue = Task.Run(async () => await SecureStorage.Default.GetAsync(“SomeKey”)).Result;
I had the same issue after upgrading to .NET 8 with
GetAwaiter().GetResult().SecureStorage.Default.GetAsync(Constants.CLAIM_OAUTH_TOKEN).GetAwaiter().GetResult();and got it working after replacing
Task.Run(async () => await SecureStorage.Default.GetAsync("SomeKey")).ResultI debugged the behaviour further. So far this is what I know:
When I build debug/release and simullator and real device, When app starts first time, previously stored value from SecureStorage is not read and returns nul from GetAsync method. But running app I can store data in SS and can get / read it normally.
A couple days ago I encountered the same error as sk1llsh0t within my log server trying to Read/Write on SecureStorage. I don’t know if this behaviour is new with .NET 8 GA as the app integration to the log server is new.
The error message is:
java.lang.NoSuchFieldError: no "Landroidx/security/crypto/EncryptedSharedPreferences$PrefKeyEncryptionScheme;" field "AES256_SIV" in class "Landroidx/security/crypto/EncryptedSharedPreferences$PrefKeyEncryptionScheme;" or its superclassesI can confirm this ONLY happens on Release build and possibly not on emulator devices. I think I was only able to reproduce the behaviour on physical devices (via Google Store, directly to device).
I ended up just using Preferences.Default for storage on Android for now. While not secure, it will at least be a workaround until this is fixed.
If it helps, my Android SDK targets are:
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="34" />Why is this issue closed? It says it was fixed in #17928 (.NET 8 SR1), but obviously, it is not.
Thank you so much @chathudan! This worked for me! 😃
Same here. Android 8.1 and .NET and MAUI.
But the
Task.Runworkaround did work!I’m also having this issue after yesterday. It was working fine even after updating to .net 8, but all of a sudden it stopped working.
The issue is still present in nightly builds. @jsuarezruiz, only the workaround above helps.
Ok my case was after logout I removed all keys
SecureStorage.Default.RemoveAll();and then after login again, the values where always available. BUT, closing and reopening the app, made the keys all vanished.So, after a few hours around this, I got to a workaround. Have to remove the keys 1 by 1 (
SecureStorage.Default.Remove("xxx");), and it works.Hope it helps others.
“When app starts first time, previously stored value” this is a contradiction. So I assume you have deinstalled the app, deployed / installed it again and THEN you get an error?
Yes, this seems to be a regression. I believe there is an open issue for this somewhere. This has nothing to do with GetAsync() afaik.
If you need to be able to install / reinstall with a working secure store the app you can set “allowBackup=false” in the AndroidManifest.xaml. I am not sure whether pre-existing secure store data can be used or if they need to be re-populated again.
For me the solution of @sk1llsh0t was not enough. Our app only lost data when got from Google Play Store. Releases built locally and from gitlab ci worked absolutely fine. Maybe Google Play Store does it’s own optimization? However, the following ProGuard rules solved our problems:
I do all my testing/development on a physical device which is where i’m seeing the issue.
PR #17928 will not be in the GA release, but is already merged and will be in the first servicing release. We are hoping to have a preview of that first servicing release available on NuGet around the same time GA is released.
Will this be in the general availability release? I think I speak for all of us when I say we’re blocked on .NET MAUI Android releases until this is released.