| MAUI-permissions-checklist | MAUI-with-SQLite | |
Preferences vs SecureStorage in .NET MAUI |
In .NET MAUI, Preferences and SecureStorage handle data persistence, but they serve very different security purposes.
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:
// 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");
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:
Limitation: Only supports string values. You must serialize objects (JSON) or convert numbers to strings.
// 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();
| 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 |
Preferences and SecureStorage in .NET MAUI — two APIs that let you persist data across app sessions.
Purpose → Store simple key-value pairs (like settings, flags, or user choices).
Storage Type → Non-sensitive data, saved in platform-specific storage:
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.
Purpose → Store sensitive data securely (tokens, passwords, API keys).
Storage Type → Encrypted storage provided by the OS:
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.
| 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 |
| MAUI-permissions-checklist | MAUI-with-SQLite | |