maui-secure-storage

Guide to adding secure storage to .NET MAUI apps using SecureStorage.Default. Covers using SetAsync, GetAsync, Remove, and RemoveAll; platform configuration (Android backup rules, iOS Keychain entitlements, Windows restrictions); common pitfalls; and a dependency-injection wrapper service for testability. Applicable to secure storage, SecureStorage, storing tokens or credentials, Keychain and Android Keystore, and sensitive data storage. Not for general file storage (use maui-file-handling), SQLite databases (use maui-sqlite-database), or authentication flows (use maui-authentication).

Last repository push
GitHub Stars
181
Forks
21
Issues / PRs
1

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "maui-secure-storage" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-cd71518c2e758205856fa435c8c900ecb8179e3071404aeba28676cc26e72417. Verify the upstream source and version against https://github.com/davidortinau/maui-skills before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Guide to adding secure storage to .NET MAUI apps using SecureStorage.Default. Covers using SetAsync, GetAsync, Remove, and RemoveAll; platform configuration (Android backup rules, iOS Keychain entitlements, Windows restrictions); common pitfalls; and a dependency-injection wrapper service for testability. Applicable to secure storage, SecureStorage, storing tokens or credentials, Keychain and Android Keystore, and sensitive data storage. Not for general file storage (use maui-file-handling), SQLite databases (use maui-sqlite-database), or authentication flows (use maui-authentication).

0 Downloads

Introduction source: third_party · davidortinau · 0.0.1 Reference

SkillPi interpretation

Tasks
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.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: maui-secure-storage
description: >
  Add secure storage to .NET MAUI apps using SecureStorage.Default.
  Covers SetAsync, GetAsync, Remove, RemoveAll, platform setup
  (Android backup rules, iOS Keychain entitlements, Windows limits),
  common pitfalls, and a DI wrapper service for testability.
  USE FOR: "secure storage", "SecureStorage", "store token securely", "Keychain",
  "Android Keystore", "save secret", "encrypted storage", "store credentials",
  "sensitive data storage".
  DO NOT USE FOR: general file storage (use maui-file-handling), SQLite databases
  (use maui-sqlite-database), or authentication flows (use maui-authentication).

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 GetAsync calls 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.Default accessed via DI wrapper, not directly from ViewModels

Current listing version

0.0.1

Verify the source revision ↗

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value85/100

Highlights

  • 提供 .NET MAUI SecureStorage 的完整使用指南,涵盖关键 API、平台陷阱和最佳实践
  • 明确的决策框架帮助用户判断是否适用 SecureStorage
  • 强调依赖注入包装提升可测试性,符合现代开发规范
Trigger Precision95/100

Highlights

  • 触发词精准完整,覆盖多种变体(secure storage, SecureStorage, Keychain 等)
  • 边界清晰,明确排除通用文件存储、SQLite 数据库等不适用场景
Structure and Standards78/100

Areas to improve

  • 缺少显式步骤划分与端到端示例 Reported severity: Minor

    文档结构良好但缺少显式的 Phase/步骤划分和端到端完整示例流程

    Recommendation: 建议增加 Step-by-Step 配置和使用示例,提升可操作性

Highlights

  • 文档结构清晰,分模块阐述平台陷阱、常见错误和最佳实践
  • 包含决策表格和检查清单,便于用户快速判断和验证
Execution Reliability68/100

Areas to improve

  • 未提供完整 DI wrapper 实现代码或配置示例 Reported severity: Major

    强调依赖注入包装但未提供完整实现代码或配置示例文件

    Recommendation: 建议在 Skill 包中提供 ISecureStorageService 接口和实现类的完整代码示例

  • 引用文件缺失 Reported severity: Minor

    主文件引用了 references/secure-storage-api.md 但未传入该文件

    Recommendation: 请确保所有被引用的文件都包含在 Skill 包中

Highlights

  • 覆盖了主要异常处理场景(Android try/catch)
  • 提及了关键的平台配置要求和限制条件
Evidence Verifiability (mean of the first four)82/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

  • Accounts, secrets or authorization

    Use a dedicated least-privilege account. Keep secrets out of public content and command logs; verify the receiving service and tenant.

    Mentioned in main document · lines 9, 86

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.