
Maui Secure Storage
- 38 installs
- 163 repo stars
- Updated July 6, 2026
- davidortinau/maui-skills
Adds secure storage to .NET MAUI apps using SecureStorage.Default with SetAsync, GetAsync, Remove, and platform Keychain/backup setup.
About
Guides adding secure storage to .NET MAUI apps via SecureStorage.Default, covering SetAsync, GetAsync, Remove, RemoveAll and platform setup for Android backup rules, iOS Keychain and Windows. A developer uses it when storing secrets or tokens securely in a MAUI app.
- SecureStorage.Default with SetAsync, GetAsync, Remove, RemoveAll
- Platform setup for Android backup rules and iOS Keychain entitlements
Maui Secure Storage by the numbers
- 38 all-time installs (skills.sh)
- Ranked #1,443 of 2,203 Security skills by installs in the Skillselion catalog
- Data as of Aug 3, 2026 (Skillselion catalog sync)
npx skills add https://github.com/davidortinau/maui-skills --skill maui-secure-storageAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 38 |
|---|---|
| repo stars | ★ 163 |
| Last updated | July 6, 2026 |
| Repository | davidortinau/maui-skills ↗ |
What it does
Adds secure storage to .NET MAUI apps using SecureStorage.Default with SetAsync, GetAsync, Remove, and platform Keychain/backup setup.
Files
Secure Storage — Gotchas & Best Practices
Critical Platform Pitfalls
⚠️ Android: Auto Backup Breaks Encrypted Values
Auto Backup restores encrypted preferences to a new device where the encryption key is invalid — this throws unrecoverable exceptions. You must either disable Auto Backup or exclude secure storage files from backup. See references/secure-storage-api.md for setup options.
⚠️ Android: Always Wrap in try/catch
Corrupted values from backup restoration throw exceptions. Never call GetAsync unprotected:
// ❌ Unprotected — crashes on corrupted backup data
var value = await SecureStorage.Default.GetAsync("key");
// ✅ Protected — handles corruption gracefully
try
{
var value = await SecureStorage.Default.GetAsync("key");
}
catch (Exception)
{
SecureStorage.Default.RemoveAll();
}⚠️ iOS: Keychain Entitlements on Simulator
Add keychain access groups for Simulator builds, but remove before physical device / App Store builds — they cause signing issues on devices where they aren't needed.
⚠️ iOS: Uninstall Does NOT Clear Keychain
Unlike Android, uninstalling an iOS app does not remove its Keychain entries. Values persist and are available if the app is reinstalled. Design for this — don't assume a fresh install means empty storage.
⚠️ iOS: iCloud Keychain Sync
Values may sync across devices via iCloud Keychain if the user has it enabled. This is platform behavior, not controllable from MAUI. Don't store device-specific tokens that shouldn't roam.
Windows Limits
- Key name: max 255 characters
- Value: max 8 KB per setting
- Composite storage: max 64 KB total
Common Mistakes
// ❌ Storing large data — SecureStorage is for small secrets only
await SecureStorage.Default.SetAsync("profile_image", base64EncodedImage);
// ✅ Store tokens, passwords, short secrets
await SecureStorage.Default.SetAsync("auth_token", jwtToken);
// ❌ Logging secret values
_logger.LogInformation("Token: {Token}", await SecureStorage.Default.GetAsync("auth_token"));
// ✅ Log existence, not value
_logger.LogInformation("Token exists: {Exists}", token is not null);
// ❌ Storing complex objects without serialization (values are strings only)
await SecureStorage.Default.SetAsync("user", userObject);
// ✅ Serialize to JSON first
await SecureStorage.Default.SetAsync("user", JsonSerializer.Serialize(user));Decision Framework
| Question | Answer |
|---|---|
| Storing a token, password, or API key? | ✅ Use SecureStorage |
| Storing user preferences or settings? | ❌ Use Preferences instead |
| Storing large files or blobs? | ❌ Use file system + encryption |
| Need cross-device sync? | ⚠️ iOS syncs via iCloud Keychain automatically |
| Need data cleared on uninstall? | ⚠️ Only works on Android, not iOS |
Testability: Always Use DI
Never call SecureStorage.Default directly from ViewModels — wrap it in an ISecureStorageService interface for testability. See references/secure-storage-api.md for the full DI wrapper pattern with mock examples.
// ❌ Direct static access — untestable
public class LoginViewModel
{
public async Task SaveToken(string token)
=> await SecureStorage.Default.SetAsync("auth_token", token);
}
// ✅ Inject interface — testable and mockable
public class LoginViewModel(ISecureStorageService secure)
{
public async Task SaveToken(string token)
=> await secure.SetAsync("auth_token", token);
}Checklist
- [ ] Android: Auto Backup disabled or secure storage excluded from backup
- [ ] Android: All
GetAsynccalls wrapped in try/catch - [ ] iOS: Keychain entitlements configured (Simulator only — remove for device builds)
- [ ] Values are strings only — complex data serialized to JSON
- [ ] No secret values logged anywhere
- [ ]
SecureStorage.Defaultaccessed via DI wrapper, not directly from ViewModels
Secure Storage API Reference
API Surface
Use SecureStorage.Default (implements ISecureStorage):
// Store
await SecureStorage.Default.SetAsync("auth_token", token);
// Retrieve (returns null if not found)
string? token = await SecureStorage.Default.GetAsync("auth_token");
// Remove single key
bool removed = SecureStorage.Default.Remove("auth_token");
// Remove all
SecureStorage.Default.RemoveAll();All values are strings only. Serialize complex data to JSON first.
---
Platform Setup
Android — Handle Auto Backup
Auto Backup can restore encrypted preferences to a new device where the encryption key is invalid, causing unrecoverable exceptions. Choose one approach:
Option A — Disable Auto Backup entirely:
In Platforms/Android/AndroidManifest.xml:
<application android:allowBackup="false" ...>Option B — Exclude secure storage from backup:
1. Create Platforms/Android/Resources/xml/auto_backup_rules.xml:
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<exclude domain="sharedpref"
path="${applicationId}.microsoft.maui.essentials.preferences.xml" />
</full-backup-content>2. Reference it in AndroidManifest.xml:
<application android:fullBackupContent="@xml/auto_backup_rules" ...>iOS / Mac Catalyst — Enable Keychain
In Platforms/iOS/Entitlements.plist (and Platforms/MacCatalyst/Entitlements.plist):
<dict>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.yourcompany.yourapp</string>
</array>
</dict>Simulator only: Add the keychain access group matching your bundle ID. Remove it before building for physical devices or App Store submission — it is not needed there and can cause signing issues.
Windows
No setup required. Limits:
- Key name: max 255 characters
- Value: max 8 KB per setting
- Composite storage: max 64 KB total
---
DI Wrapper Service for Testability
Define the interface
public interface ISecureStorageService
{
Task SetAsync(string key, string value);
Task<string?> GetAsync(string key);
bool Remove(string key);
void RemoveAll();
}Implement against SecureStorage.Default
public class SecureStorageService : ISecureStorageService
{
public Task SetAsync(string key, string value)
=> SecureStorage.Default.SetAsync(key, value);
public async Task<string?> GetAsync(string key)
{
try
{
return await SecureStorage.Default.GetAsync(key);
}
catch (Exception)
{
// Corrupted value — clear and return null
SecureStorage.Default.RemoveAll();
return null;
}
}
public bool Remove(string key)
=> SecureStorage.Default.Remove(key);
public void RemoveAll()
=> SecureStorage.Default.RemoveAll();
}Register in MauiProgram.cs
builder.Services.AddSingleton<ISecureStorageService, SecureStorageService>();Inject into view models
public class LoginViewModel
{
private readonly ISecureStorageService _secure;
public LoginViewModel(ISecureStorageService secure)
{
_secure = secure;
}
public async Task SaveTokenAsync(string token)
{
await _secure.SetAsync("auth_token", token);
}
public async Task<string?> GetTokenAsync()
{
return await _secure.GetAsync("auth_token");
}
}Mock in tests
var mock = new Mock<ISecureStorageService>();
mock.Setup(s => s.GetAsync("auth_token"))
.ReturnsAsync("test-token-value");
var vm = new LoginViewModel(mock.Object);