Open Closed

OAuthSecureStorage missing AccessTokenKey #7487


User avatar
0
improwise created
  • ABP Framework version: v8.2
  • UI Type: MAUI Blazor

When trying to build a MAUI Blazor (and probably MAUI Xamarin as well I guess) you will get build errors from OAuthSecureStorage as the AccessTokenKey property is not defined.

using Volo.Abp.Account.Pro.Public.MauiBlazor.OAuth;
using Volo.Abp.DependencyInjection;
namespace ImproWise.Koll.MauiBlazor;
[Volo.Abp.DependencyInjection.Dependency(ReplaceServices = true)]
[ExposeServices(typeof(IOAuthSecureStorage))]
public class OAuthSecureStorage : IOAuthSecureStorage, ITransientDependency
{

 public Task SetAsync(string key, string value)
 {
#if DEBUG
 Preferences.Set(key, value);
 return Task.CompletedTask;
#else
 return SecureStorage.Default.SetAsync(key, value);
#endif
 }
 public Task\<string> GetAsync(string key)
 {
#if DEBUG
 return Task.FromResult(Preferences.Get(key, string.Empty));
#else
** return SecureStorage.Default.GetAsync(AccessTokenKey);**
#endif
 }
 public Task RemoveAsync(string key)
 {
#if DEBUG
 Preferences.Remove(key);
#else
** SecureStorage.Default.Remove(AccessTokenKey);**
#endif
 return Task.CompletedTask;
 }
}\

The only thing close to the documentation we have been able to find about this is:

https://docs.abp.io/en/commercial/latest/getting-started-maui

Which points to:

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/secure-storage?view=net-maui-8.0&tabs=android

but I don't see how that could make it work without changing the provided code.

You can make it build by doing something like

string AccessTokenKey = "1234";

but that isn't really a solution to the problem.

Is this a bug or something we should handle ourselves and if so, the documentation probably needs updating.


9 Answer(s)
  • User Avatar
    0
    improwise created

    I thought this sounded familiar, apparently we had a similar discussion 2 years ago

    https://support.abp.io/QA/Questions/3714/NET-MAUI-mobile-application-uses-Preferences-instead-of-SecureStorage-for-JWT-Tokens

    But I guess the question still stands about how to solve this and make the ABP generated solution build.

  • User Avatar
    0
    improwise created

    A somewhat related issue to this so I put it in here instead of creating a new issue about it.

    The code generated by ABP Suite (and probably the normal MAUI projects in all version) does not seem to be compatible with newer versions of Android. When you try to build it in Release mode with SDK 33 or higher (which is a requirement these days), you will get this error message:

    android:exported needs to be explicitly specified for element <activity#XXXXWebAuthenticatorCallbackActivity>. Apps targeting Android 12 and higher are required to specify an explicit value for android:exported when the corresponding component has an intent filter defined.

    After some investigation, it turns out to be the generated WebAuthenticatorCallbackActivity that is the reason for this, and the solution is to put android:exported in the Activity decoration

    [Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)]
    [IntentFilter(new[] { Intent.ActionView },
        Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
        DataScheme = CALLBACK_SCHEME)]
    public class XXXXWebAuthenticatorCallbackActivity : WebAuthenticatorCallbackActivity
    {
        public const string CALLBACK_SCHEME = "XXXX";
    }
    
  • User Avatar
    0
    improwise created

    Still waiting for a reply here.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    When trying to build a MAUI Blazor (and probably MAUI Xamarin as well I guess) you will get build errors from OAuthSecureStorage as the AccessTokenKey property is not defined.

    I can't reproduce the problem.

    • abp new Qa -u maui-blalzor
    • dotnet build

    The code generated by ABP Suite (and probably the normal MAUI projects in all version) does not seem to be compatible with newer versions of Android. When you try to build it in Release mode with SDK 33 or higher (which is a requirement these days), you will get this error message:

    Thanks. We will update the template, and your ticket will be refunded.

  • User Avatar
    0
    improwise created

    Hi,

    When trying to build a MAUI Blazor (and probably MAUI Xamarin as well I guess) you will get build errors from OAuthSecureStorage as the AccessTokenKey property is not defined.

    I can't reproduce the problem.

    • abp new Qa -u maui-blalzor
    • dotnet build

    Did you build it in Release mode as that is needed to activate the mentioned code? Will build fine in debug mode but not in Release mode as AccessTokenKey isn't declared.

    public Task&lt;string> GetAsync(string key) { #if DEBUG return Task.FromResult(Preferences.Get(key, string.Empty)); #else return SecureStorage.Default.GetAsync(AccessTokenKey); #endif } public Task RemoveAsync(string key) { #if DEBUG Preferences.Remove(key); #else SecureStorage.Default.Remove(AccessTokenKey); #endif return Task.CompletedTask; }

    The code generated by ABP Suite (and probably the normal MAUI projects in all version) does not seem to be compatible with newer versions of Android. When you try to build it in Release mode with SDK 33 or higher (which is a requirement these days), you will get this error message:

    Thanks. We will update the template, and your ticket will be refunded.

    Thanks.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Did you build it in Release mode as that is needed to activate the mentioned code? Will build fine in debug mode but not in Release mode as AccessTokenKey isn't declared.

    Ok, i will try

  • User Avatar
    1
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    i will fix the problem

    you can try

    [Volo.Abp.DependencyInjection.Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IOAuthSecureStorage))]
    public class OAuthSecureStorage : IOAuthSecureStorage, ITransientDependency
    {
        public Task SetAsync(string key, string value)
        {
    #if DEBUG
            Preferences.Set(key, value);
            return Task.CompletedTask;
    #else
            return SecureStorage.Default.SetAsync(key, value);
    #endif
        }
    
        public Task<string> GetAsync(string key)
        {
    #if DEBUG
            return Task.FromResult(Preferences.Get(key, string.Empty));
    #else
            return SecureStorage.Default.GetAsync(key);
    #endif
        }
    
        public Task RemoveAsync(string key)
        {
    #if DEBUG
            Preferences.Remove(key);
    #else
            SecureStorage.Default.Remove(key);
    #endif
            return Task.CompletedTask;
        }
    }
    
  • User Avatar
    0
    improwise created

    Hi,

    Ah, so it is only a "typo" where it should have been "key" but instead was "AccessTokenKey"? Seems reasonable when looking at the code more in detail now, was a bit stressed last time around just trying to get it to work and the name AccessTokenKey made it look like something more than just the usual keypair.

    Is storage actually used anywhere in the MAUI app currently?

    Please also note the part about Exported = true being needed (or implemented / solved elsewhere)

    BTW, at least for me, the small emoji icon seems broken now but worked fine before, so you will have to imagine a thumbs up here :)

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Please also note the part about Exported = true being needed (or implemented / solved elsewhere)

    yes, added

    Is storage actually used anywhere in the MAUI app currently?

    this is only used to store the access_token and refresh_token

Made with ❤️ on ABP v9.1.0-preview. Updated on November 18, 2024, 05:54