xamarin.ios - 使用 Monotouch 存储和读取钥匙串(keychain)的密码

标签 xamarin.ios

编辑:问题已解决 .为了对社区有所贡献,我在我的博客上设置了一些辅助方法和一个如何使用它们的示例。在这里找到它KeyChain MT example

--
原始问题:

在模拟器和 iPad 上运行 iOS4.2。

我正在尝试使用下面的代码从钥匙串(keychain)中存储和读取密码。我的代码灵感是https://github.com/ldandersen/scifihifi-iphone/但我无法让它工作。我错过了什么?

// Create a record.
SecRecord o = new SecRecord ( SecKind.GenericPassword );
o.Service = "myService";
o.Label = "myService";
o.Account = "test@test.com";
// The super secret password.
o.Generic = NSData.FromString ( "secret!", NSStringEncoding.UTF8 );
// Add to keychain.
SecKeyChain.Add ( o );

// Now cerate another recored to query what we just saved.  
o = new SecRecord ( SecKind.GenericPassword );
o.Service = "myService";
o.Account = "test@test.com";

// Query as record.         
SecStatusCode code;
var data = SecKeyChain.QueryAsRecord ( o, out code );

// This will tell us "all good!"... 
Console.WriteLine ( code );

// But data.Generic is NULL and this line will crash. :-(
Console.WriteLine ( NSString.FromData ( data.Generic, NSStringEncoding.UTF8 ) );

最佳答案

而不是使用SecRecord.ValueData试试这个:

Console.WriteLine(NSString.FromData(data.Generic, NSStringEncoding.ASCIIStringEncoding));
GenericSecKind.GenericPassword 返回 NSData的被存储。

关于xamarin.ios - 使用 Monotouch 存储和读取钥匙串(keychain)的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4852108/

相关文章:

c# - RowSelected - NullReferenceException

ios - 升级到 MonoTouch 6 后自动旋转停止工作

xamarin.ios - Xamarin.Forms.DatePicker 文本颜色

ios - Xamarin.iOS MvvmCross,后退导航按钮和选项卡

ios - 从 Storyboard 实例化时,未应用 Interface Builder 中定义的样式元素

c# - Xamarin Async ViewDidAppear 在 ViewDidLoad 期间调用

c# - 使用 ReactiveUI 禁用和启用文本字段更改上的按钮

mvvm - 为什么 Xamarin 跨平台使用 MVVM

iphone - 在 iPhone/iPad 项目中处理 WCF

ios - 谷歌分析绑定(bind)有什么独家新闻?