c# - 如何在 Xamarin iOS 中将本地钥匙串(keychain)同步到设备和 iCloud

标签 c# ios xamarin xamarin.ios

对于我的应用程序,当用户登录时,我会显示一条提示并询问用户是否要将密码保存到钥匙串(keychain)。代码保存成功,我可以完全正常地查询钥匙串(keychain)以获取存储的密码。

我的问题是,当我在“密码和帐户 - 网站和应用程序密码”下检查手机时,为什么没有显示任何内容。

enter image description here

我用来保存 key 的代码

public static bool SaveValueToKeyChain(string entryKey, string entryValue)
{
    SecRecord record = new SecRecord(SecKind.GenericPassword)
    {
        Account = entryKey,
        Label = entryKey,
        Service = _keyChainServiceName
    };

    SecStatusCode resultCode;
    SecKeyChain.QueryAsRecord(record, out resultCode);

    if (resultCode == SecStatusCode.Success)
    {
        if (SecKeyChain.Remove(record) != SecStatusCode.Success)
        {
            return false;
        }
    }

    resultCode = SecKeyChain.Add(new SecRecord(SecKind.GenericPassword)
    {
        Label = entryKey,
        Account = entryKey,
        Service = _keyChainServiceName,
        Accessible = SecAccessible.WhenUnlockedThisDeviceOnly,
        Synchronizable = true,
        ValueData = NSData.FromString(entryValue, NSStringEncoding.UTF8),

    });


    return resultCode == SecStatusCode.Success;
}

我也遵循了这里的解决方案 https://developer.xamarin.com/samples/monotouch/Keychain/也没有运气。我可以得到一些帮助吗?

最佳答案

对于每个应用程序,钥匙串(keychain)都有两个访问区域,一个私有(private)区域和一个公共(public)区域(即组模式)。

私有(private)区域:是一个封闭的存储区域。每个应用程序只能操作自己的私有(private)区域。此应用程序中存储的任何数据对于其他程序都是不可见的,并且其他程序无权访问此私有(private)区域。 (可以理解为带有钥匙串(keychain)的沙箱)。

公共(public)区域:苹果提供了同一开发者帐户开发的多个应用程序之间的数据共享模块。现在仅限于同一开发者帐户下不同应用程序之间的数据共享。该区域是独立于私有(private)区域的另一个数据存储空间。实现多个应用之间对部分数据的共同访问。

我猜这里应该是私有(private)区域,你可以尝试一下群组模式。 Sharing Access to Keychain Items Among a Collection of Apps

关于c# - 如何在 Xamarin iOS 中将本地钥匙串(keychain)同步到设备和 iCloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57209938/

相关文章:

c# - 最大长度条件的 Asp.net Identity PasswordValidator

c# 如何在使用 list.Remove 时遍历列表中的每个项目

ios - iOS或MonoTouch固有内存泄漏?

iOS:从浏览器后台播放?

ios - MPNowPlayingInfoCenter 适用于模拟器,但不适用于设备

c# - ASP.NET - IE8 中控件位置的问题

c# - 如何使用脚本或代码控制媒体播放器

ios - 无法将 WKWebView 添加到 Xcode 项目

c# - Xamarin Google Cloud Messaging GcmClient.CheckDevice(this) 导致运行时错误

c# - 使 ImageView 循环