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 9 months ago
  • Reactions: 1
  • Comments: 23 (5 by maintainers)

Most upvoted comments

Using as below solved my problem:

var someSecureValue = Task.Run(async () => await SecureStorage.Default.GetAsync(“SomeKey”)).Result;

I debugged the behaviour further. So far this is what I know:

  1. It is an issue relating to Secure Storage. Possibly related to the newest changes.
  2. In my case it seems to occur when accessing the Secure Storage during app startup (App.xaml.cs constructor and code being called from there) and blocking the app startup progression (using Wait() or similar) until data is received. Not doing so (meaning: using async calls) will return data.

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.

I think I was only able to reproduce the behaviour on physical devices

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.