dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.27k stars 1.76k forks source link

SecureStorage returns null for all values after restarting app (Android) #23013

Open pmigliore opened 5 months ago

pmigliore commented 5 months ago

Description

If I set a value in the SecureStorage await SecureStorage.SetAsync("TestKey", "TestValue") during the first app startup, and then return the value await SecureStorage.GetAsync("TestKey"), the value is returned correctly. But if I close the app, even from running in the background, and restart it a second time, and call await SecureStorage.GetAsync("TestKey"), then it returns null. But if I set the value again, then it starts working successfully, and returns the value corectly if I restart the app a third time and so on.

Steps to Reproduce

  1. Create a new .NET MAUI project.
  2. In the App.xaml.cs constructor add the following line of code: Task.Run(async () => { await SecureStorage.SetAsync("TestKey", "TeskKey"); var val = await SecureStorage.GetAsync("TeskKey"); }); and add a breakpoint in the last line of code.
  3. Start the Android (API 30) emulator in debug mode, "val" should return the correct value
  4. Close the app, even from the background, and comment out await SecureStorage.SetAsync("TestKey", "TeskKey");.
  5. Restart the app in debug mode.
  6. If running it again "val" returns "null"

Link to public reproduction project repository

No response

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12 and below

Did you find any workaround?

No

Relevant log output

None
github-actions[bot] commented 5 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

ninachen03 commented 5 months ago

I can repro this issue on the latest 17.11.0 Preview 2.1 (8.0.60 & 8.0.40 & 8.0.21). image

Domik234 commented 2 weeks ago

@ninachen03, @samhouts Hey, I think this should always return null.

SetAsync is used to save value TeskKey to key TestKey. GetAsync after is calling for key TeskKey but this key won't exist because SetAsync for this key was never used.

Key Value
TestKey TeskKey
TeskKey null

For a relevant test you should call var val = await SecureStorage.GetAsync("TestKey");

(this case tested on 8.0.82 - requested was for 8.0.40 SR5): Physical Device API 31 Image

Emulator API 30 Image

I believe this issue should be propably tagged as (s/needs-info) because it looks like it's no more needed.