maui-secure-storage

使用 SecureStorage.Default 为 .NET MAUI 应用添加安全存储的指南。涵盖 SetAsync、GetAsync、Remove、RemoveAll 的使用,平台配置(Android 备份规则、iOS Keychain 权限、Windows 限制)、常见陷阱,以及用于可测试性的依赖注入包装服务。适用场景包括安全存储、SecureStorage、保存令牌或凭据、Keychain 与 Android Keystore 等敏感数据存储。非适用:通用文件存储(请使用 maui-file-handling)、SQLite 数据库(请使用 maui-sqlite-database)、身份验证流程(请使用 maui-authentication)。

仓库最近推送
GitHub Stars
181
Forks
21
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "maui-secure-storage":https://skillpi.com/zh/skills/deepskill-cd71518c2e758205856fa435c8c900ecb8179e3071404aeba28676cc26e72417。安装前核对上游来源和版本:https://github.com/davidortinau/maui-skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

使用 SecureStorage.Default 为 .NET MAUI 应用添加安全存储的指南。涵盖 SetAsync、GetAsync、Remove、RemoveAll 的使用,平台配置(Android 备份规则、iOS Keychain 权限、Windows 限制)、常见陷阱,以及用于可测试性的依赖注入包装服务。适用场景包括安全存储、SecureStorage、保存令牌或凭据、Keychain 与 Android Keystore 等敏感数据存储。非适用:通用文件存储(请使用 maui-file-handling)、SQLite 数据库(请使用 maui-sqlite-database)、身份验证流程(请使用 maui-authentication)。

0 下载

介绍来源: third_party · davidortinau · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
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

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值85/100

亮点

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

亮点

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

待提升项

  • 缺少显式步骤划分与端到端示例 原报告严重程度:轻微

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

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

亮点

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

待提升项

  • 未提供完整 DI wrapper 实现代码或配置示例 原报告严重程度:重大

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

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

  • 引用文件缺失 原报告严重程度:轻微

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

    改进建议:请确保所有被引用的文件都包含在 Skill 包中

亮点

  • 覆盖了主要异常处理场景(Android try/catch)
  • 提及了关键的平台配置要求和限制条件
证据可验 (前四项均值)82/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

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

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

  • 账号、密钥或授权

    使用专用、最小权限账号,不把密钥粘贴到公开内容或命令日志;核对接收数据的服务与租户。

    主文档提及 · 第 9, 86 行

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。