You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Essentials Secure Storage存储JWT令牌报错求助

Hey there! Let's work through this issue together—since you're new to Xamarin and C#, these platform-specific NuGet quirks can definitely throw you for a loop, so no worries at all.

Core Problem Breakdown

The Xamarin.Essentials.NotImplementedInReferenceAssemblyException you're hitting is almost always tied to one of two things:

  1. You haven't installed the Xamarin.Essentials NuGet package in your main macOS application project (only in your shared/.NET Standard class library). The platform-specific code lives in the main project, so the class library can't access it otherwise.
  2. Your macOS project type might not support SecureStorage (e.g., using the older Classic Xamarin.Mac framework instead of Modern).

Step-by-Step Fixes to Try

Let's go through targeted fixes beyond what you've already attempted:

1. Verify NuGet Installation Across All Projects

  • Uninstall Xamarin.Essentials from all your projects (class libraries + main macOS app).
  • Reinstall the package first in your main macOS application project, then in your shared/.NET Standard class libraries. This ensures the platform-specific implementation gets loaded correctly.
  • Double-check the .csproj files for both projects to confirm they have matching PackageReference entries for Xamarin.Essentials, like:
    <PackageReference Include="Xamarin.Essentials" Version="1.8.0" /> <!-- Use the latest compatible version -->
    

2. Fix the Async/Await Mismatch in Your Code

Looking at your TokenStorageController.SaveToken method—it's a synchronous method (no async/Task return type), but you're calling it with await in your RestService. This can cause unexpected thread issues, especially when combined with Task.Run(...).Result (which can lead to deadlocks).

Update your call to remove the await:

// RestService code fix
TokenStorageController tokenStorage = new TokenStorageController();
bool saveSuccess = tokenStorage.SaveToken("boommmmm"); // No await needed here

Also, avoid using .Result on async tasks if possible—switch to await instead:

// Replace this:
var loginTask = Task.Run(() => restService.LoginAsync(user)).Result;

// With this (make the containing method async):
var loginResult = await restService.LoginAsync(user);

3. Check Your macOS Project Framework Type

Xamarin.Essentials' SecureStorage requires the Modern Xamarin.Mac framework (not Classic). To confirm:

  • Right-click your macOS project → Select Options → Go to the General tab.
  • Check that the "Project Format" is set to Modern. If it's Classic, you'll need to migrate to Modern to use SecureStorage (or fall back to an alternative storage method).

4. Test SecureStorage Directly in the Main Project

To rule out class library reference issues, add a test call directly in your macOS app's main entry point (e.g., AppDelegate.cs):

async void TestSecureStorage()
{
    try
    {
        await SecureStorage.SetAsync("oauth_token", "test_token");
        var token = await SecureStorage.GetAsync("oauth_token");
        Console.WriteLine($"Stored token: {token}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

Call this method on app launch—if it throws the same exception, it's definitely a platform support or NuGet configuration issue.

Alternative Plan If SecureStorage Fails

If you still can't get SecureStorage working on macOS, your backup plan of using SQL storage is totally reasonable. For simpler non-highly-sensitive data, you could also stick with Preferences (which you're already testing) since it's more lightweight and has broader platform support.


内容的提问来源于stack exchange,提问作者Khal_Tech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:00:23