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.11k stars 1.73k forks source link

Secure Storage GetAsync returns null in MAUI .NET 7 Android #18914

Closed MikeP0911 closed 10 months ago

MikeP0911 commented 11 months ago

Description

When getting the values stored using SetAsync is returning null.

public async void SetValueKeyChain(string key, string value)
{
    try
    {         
        await Task.Run(() =>
        {
            SecureStorage.Default.SetAsync(key, value);

        }).ConfigureAwait(false);
        await Task.Delay(30);

    }catch (Exception ex)
    {        
    }
}
public async void GetKeyAsync(string keyName)
{
    try
    {
        KeyVal = await SecureStorage.Default.GetAsync(keyName);
    }
    catch (Exception ex)
    {       
    }
}

when trying to access through Get it does not throw any exception. but returns null. var Token = GetValueKeyChain(DToken);

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

alzubitariq commented 11 months ago

@MikeP0911 , in order to make sure the internal task is already completed you have to await it , so your code should be like this

public async void SetValueKeyChain(string key, string value)
{
    try
    {         
        await Task.Run(async() =>
        {
              await SecureStorage.Default.SetAsync(key, value);
        }).ConfigureAwait(false);

        await Task.Delay(30);

    }
    catch (Exception ex)
    {        
    }
}
MikeP0911 commented 11 months ago

@alzubitariq Thank you for your reply. I tried implementing your suggestion. But still the problem persists. When I get the value it is empty.

Help is appreciated.

MikeP0911 commented 11 months ago

@alzubitariq My get is as following please let know if there is any problem.

public string GetValueKeyChain(string key)
{
    try
    {
        KeyVal = string.Empty;
        GetKeyAsync(key);        
    }
    catch (Exception ex)
    {
    }
    return KeyVal;
}
public async void GetKeyAsync(string keyName)
{
    try
    {
        KeyVal = await SecureStorage.Default.GetAsync(keyName);                
    }
    catch (Exception ex)
    {
    }

}
alzubitariq commented 11 months ago

@MikeP0911 , why try to do things "synchronous", I suggest u to make things more simple like

string GetValueKeyChain(string key)
{
    var result = "";
    Task.Run(async () => result = await SecureStorage.Default.GetAsync(key)).Wait();
    return result;
}

void SetValueKeyChain(string key, string value)
{
    Task.Run(async () => await SecureStorage.Default.SetAsync(key, value)).Wait();
}

and you have to make sure SecureStorage instance is not null , just try to set or get after window "created" event or page Loaded event

MikeP0911 commented 10 months ago

@alzubitariq Thank you for the help. The issue is resolved.