无法访问保存在 Xamarin.iOS 钥匙串值中 [英] Can't access saved in a Xamarin.iOS keychain value
问题描述
在我的应用程序中,我有两种方法可以帮助我从钥匙串中保存和获取值
In my app I have two methods which help me save and get values from keychain
public static void StoreKeysInKeychain(string key, string value)
{
DeleteKeyFromKeychain(key);
var s = new SecRecord(SecKind.GenericPassword)
{
ValueData = NSData.FromString(value),
Generic = NSData.FromString(key)
};
SecKeyChain.Add(s);
Console.WriteLine(GetRecordsFromKeychain(key));
}
public static string GetRecordsFromKeychain(string key)
{
SecStatusCode res;
var rec = new SecRecord(SecKind.GenericPassword)
{
Generic = NSData.FromString(key)
};
var match = SecKeyChain.QueryAsRecord(rec, out res);
if (match != null)
return match.ValueData.ToString();
return "Error";
}
我在那里保存了两个值,出于某种原因,当我尝试获取其中任何一个时,我得到一个错误"字符串而不是值.相同的代码以前工作没有任何问题,但后来我添加了另一个参数来存储在钥匙串中并重命名两个
I'm saving there two values and for some reason when I try to get any of them I get an "Error" string instead of the value. The same code previously worked without any problems, but then I added another parameter to store in the keychain and renamed both
推荐答案
如果您的目标是 iOS 10+,请确保您正在检查 SecKeyChain.Add
的返回状态:
If you are targeting iOS 10+, make sure you are checking the return status of your SecKeyChain.Add
:
var status = SecKeyChain.Add(s);
if (status == SecStatusCode.Success)
Console.WriteLine(GetRecordsFromKeychain(key));
else
Console.WriteLine(status);
如果你得到一个 -34018
,那么你需要在你的 Entitlements.plist
中启用 KeyChain 访问:
If you are getting a -34018
, then you need to enable KeyChain access in your Entitlements.plist
:
然后确保将 Entitlements.plist
分配给您的项目 Build
/iOS Bundle Signing 中的
Custom Entitlements
代码>:
And then make sure that the Entitlements.plist
is assigned to your Custom Entitlements
in your Project Build
/ iOS Bundle Signing
:
这篇关于无法访问保存在 Xamarin.iOS 钥匙串值中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!