ios - SecKeyChain 项目和 SecRecord 在设备和模拟器上调试之间的差异,Xamarin iOS

标签 ios xamarin.ios keychain

我正在 Xamarin.iOS 中创建一个应用程序,它使用 Security 的 SecKeyChain。在其中,我试图将用户的凭据作为 SecRecord 保存到 KeyChain,然后再访问它。当应用启动时,它会检查KeyChain中是否有保存的凭据,并决定是否提示手动登录。

调用 SecKeyChain.Add 时,返回成功。但是,在强制关闭应用程序 SecKeyChain.QueryAsRecord 后失败,错误代码为 AuthFailed。这只发生在设备上,而模拟器会成功。

另一个问题与创建的 SecRecord 有关。在 Device 上调试时创建的对象之间存在严重差异和 Simulator .创建 SecRecord 的代码是:

var credentialsRecord = new SecRecord(SecKind.GenericPassword)
        {
            Generic = NSData.FromString("record"),
            Label = credentials.Username,
            Account = credentials.Username,
            Service = CredentialsStorageServiceName,
            ValueData = NSData.FromBytes(credentials.Password.ToIntPtr(), Convert.ToUInt32(credentials.Password.Length()) * 2),
            AccessControl = new SecAccessControl(SecAccessible.WhenPasscodeSetThisDeviceOnly, SecAccessControlCreateFlags.TouchIDCurrentSet)
        };
var statusCode = SecKeyChain.Add(credentialsRecord);

在 entitlements.plist 中,我启用了钥匙串(keychain)访问组,并添加了一个与我的虚拟 Xcode 项目的包标识符同名的组。在项目选项 -> iOS 包签名中,我有一个签名身份和一个配置文件,自定义权利字段为空。

我是否缺少访问设备钥匙串(keychain)的内容,或者该问题是否与其他问题有关?

如果我遗漏了什么,请告诉我,在此先感谢您。

最佳答案

我对 Xamarin 不是很熟悉,但由于它用 C# 包装了 iOS native API,Apple 自己的示例可能会有所帮助(因为它涵盖了与您描述的基本相同的场景):https://developer.apple.com/library/content/documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html#//apple_ref/doc/uid/TP30000897-CH208-SW1

此外,查看 SecAccessControlCreateWithFlags ( https://developer.apple.com/reference/security/1394452-secaccesscontrolcreatewithflags ) 的 Apple 文档可能会有用,因为您正在使用自定义保护和标志

以下是您正在使用的组合的摘录:

SecAccessible.WhenPasscodeSetThisDeviceOnly 保护意味着:

The data in the keychain can only be accessed when the device is unlocked. Only available if a passcode is set on the device.

SecAccessControlCreateFlags.TouchIDCurrentSet 标志表示:

Constraint to access with Touch ID for currently enrolled fingers. Touch ID must be available and enrolled with at least one finger. Item is invalidated if fingers are added or removed.

至于在模拟器中使用钥匙串(keychain),我不会依赖那里的一致行为。也没有 TouchID 或密码,因此使用它们的钥匙串(keychain)项目属性和访问控制标志(根据您的情况)将不起作用。

长话短说,在使用钥匙串(keychain)开发安全性时,最安全的方法是在实际设备上进行,以确保应用在用户手中按预期运行。但是,如果您想在模拟器上进行开发,请使用默认标志(或使用苹果的示例作为灵感)并在转移到生产环境时切换到更安全的标志。

关于ios - SecKeyChain 项目和 SecRecord 在设备和模拟器上调试之间的差异,Xamarin iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41468743/

相关文章:

ios - swift/iOS : How to use inout parameters in functions with AnyObject/Any or Pointers

ios - 从后台打开应用程序时不调用 Xamarin iOS ViewDidAppear

ios - 在 iOS 应用程序中存储用户信息的最佳方式

ios - 使用 PhotoLibrary 私有(private)框架设置 iOS 墙纸时裁剪/缩放不起作用

ios - 有没有办法在联网时回拨?

iOS 可拖动 subview

xamarin.ios - 尝试使用 Transform 在 MonoTouch 中旋转 UIView

tomcat - 异常 : Public keys in reply and keystore don't match

ios - 如何从 iOS 下钥匙串(keychain)中存在的证书中解码序列号、发行者信息等?

iphone - 如何使 iOS 5 中的亮度变化永久化?