Previous MAUI-permissions-checklist MAUI-with-SQLite Next

Preferences vs SecureStorage in .NET MAUI

In .NET MAUI, Preferences and SecureStorage handle data persistence, but they serve very different security purposes.

1️⃣ Preferences (Unencrypted)

Best for light, non-sensitive data like UI themes, font sizes, or Last Login timestamps. It stores data in a simple Key-Value pair format.

Supported Types: bool, double, int, float, long, string, DateTime.

Storage Location:

  • Android → SharedPreferences
  • iOS → NSUserDefaults
  • Windows → ApplicationDataContainer

Example:

// Saving data
Preferences.Default.Set("theme_color", "Dark");

// Retrieving data (with default)
string theme = Preferences.Default.Get("theme_color", "Light");

// Removing data
Preferences.Default.Remove("theme_color");
    

2️⃣ 🔒 SecureStorage (Encrypted)

Essential for sensitive information like Auth Tokens, Passwords, or API Keys. It encrypts data before saving it to the device's secure hardware.

Security Mechanisms:

  • Android → Keystore
  • iOS/macOS → Keychain
  • Windows → Data Protection API

Limitation: Only supports string values. You must serialize objects (JSON) or convert numbers to strings.

Example:

// Saving a token
await SecureStorage.Default.SetAsync("oauth_token", "secret_value_123");

// Retrieving a token
string token = await SecureStorage.Default.GetAsync("oauth_token");

// Clearing all secure data
SecureStorage.Default.RemoveAll();
    

📊 Key Comparison

Feature Preferences SecureStorage
Security None (Plain Text) High (Encrypted)
Performance Very Fast Slightly Slower (Encryption overhead)
Async Support Synchronous Asynchronous (Task)
Best Use Case User Settings Authentication & PII

⚙️ Platform-Specific Requirements

  • Android: To use SecureStorage, you must have Auto Backup configured correctly if you want data to persist across reinstalls.
  • iOS: You may need to enable Keychain Sharing in your Entitlements.plist for certain scenarios.

🎯 Advanced Topics

  • 🔄 JSON-serialize a User Profile into SecureStorage
  • ⚙️ Unified Settings Wrapper class combining Preferences + SecureStorage
  • 🔐 Biometric authentication guard before SecureStorage access

Preferences and SecureStorage in .NET MAUI — two APIs that let you persist data across app sessions.

⚙️ Preferences

Purpose → Store simple key-value pairs (like settings, flags, or user choices).

Storage TypeNon-sensitive data, saved in platform-specific storage:

  • Android → SharedPreferences
  • iOS/macOS → NSUserDefaults
  • Windows → ApplicationDataContainer

Usage

using Microsoft.Maui.Storage;

// Save
Preferences.Set("theme", "dark");

// Retrieve
var theme = Preferences.Get("theme", "light"); // default = "light"

// Remove
Preferences.Remove("theme");

// Clear all
Preferences.Clear();
    

Best For → Settings, flags, app preferences, non-sensitive user data.

🔒 SecureStorage

Purpose → Store sensitive data securely (tokens, passwords, API keys).

Storage TypeEncrypted storage provided by the OS:

  • Android → KeyStore
  • iOS/macOS → Keychain
  • Windows → Credential Locker

Usage

using Microsoft.Maui.Storage;

// Save securely
await SecureStorage.SetAsync("auth_token", "12345");

// Retrieve securely
var token = await SecureStorage.GetAsync("auth_token");

// Remove
SecureStorage.Remove("auth_token");
    

Best For → Authentication tokens, credentials, sensitive app data.

📊 Comparison Table

Feature Preferences SecureStorage
Data type Key-value pairs Key-value pairs
Security Plain storage Encrypted storage
Use case Non-sensitive settings Sensitive data (tokens, passwords)
Platform store SharedPreferences / NSUserDefaults / AppDataContainer KeyStore / Keychain / Credential Locker

💡 Best Practices

  • Use Preferences for lightweight, non-sensitive data (like theme, language).
  • Use SecureStorage for sensitive data (like login tokens).
  • Always check availability of SecureStorage (some devices may restrict it).
  • Handle exceptions gracefully (e.g., when storage is unavailable).
Back to Index
Previous MAUI-permissions-checklist MAUI-with-SQLite Next
*