如何将Xamarin应用的AccountStore迁移至MAUI?原方法是否可行?
Xamarin AccountStore 迁移至 MAUI 方案说明
那个Xamarin.Forms迁移到SecureStorage的方法大部分适用,只需针对MAUI做少量适配,具体如下:
核心逻辑复用
原迁移助手类中读取AccountStore的代码可以直接复用,Xamarin.Auth的AccountStore组件支持.NET 6+,可直接在MAUI项目中引用。读取到Account对象后,转存到MAUI内置的SecureStorage即可,调用方式和Xamarin.Essentials完全一致,无需额外依赖。
需调整的平台配置
- iOS:确保MAUI项目的
Entitlements.plist配置了与旧Xamarin应用完全相同的钥匙串访问组,否则无法读取旧AccountStore存储的钥匙串数据。 - Android:若MAUI项目包名与旧应用一致,可直接读取旧AccountStore默认存储路径
/data/data/[包名]/files/accounts.json;若包名变更,需处理跨包文件访问权限,或通过应用备份迁移数据。
迁移代码示例
// 读取旧AccountStore中的账户 var accountStore = AccountStore.Create(); var accounts = await accountStore.FindAccountsForServiceAsync("你的服务标识"); // 转存到MAUI SecureStorage foreach (var account in accounts) { await SecureStorage.SetAsync("auth_username", account.Username); if (account.Properties.TryGetValue("access_token", out var token)) { await SecureStorage.SetAsync("auth_token", token); } // 按需存储其他账户属性 } // 可选:迁移完成后清除旧AccountStore数据 foreach (var account in accounts) { await accountStore.DeleteAsync(account, "你的服务标识"); }
注意事项
- 务必在真实设备上测试验证,模拟器的钥匙串/文件系统行为可能与真机存在差异。
- 若旧应用使用了自定义AccountStore存储路径,需对应调整读取逻辑。
- 迁移完成后,建议移除Xamarin.Auth依赖,统一使用MAUI的
SecureStorage管理敏感数据。
内容的提问来源于stack exchange,提问作者t_m27
相关产品推荐
相关产品推荐

