我目前正在开发多个跨平台应用程序(在iOS下)使用一些共享的钥匙串条目。我目前的项目开始在Android上,在我们有一个工作版本后,我继续研究iOS版本。我从早期项目导入了我们的钥匙串访问代码,以访问我们的共享登录数据。只有这次查询总是返回SecStatusCode.ItemNotFound。
我比较了配置文件和权利,所有看起来都一样。一段时间后它开始让我疯了,我创建了一个新的空应用程序,只有钥匙串代码,相同的包标识符,配置文件和权利文件作为当前不工作的应用程序,它工作正常并返回我的数据。
对于我的问题,除了entitlements.plist和配置文件之外,还有哪些地方需要配置,可能会干扰我对钥匙串条目的访问?由于项目有点大,我不想将所有代码复制到新项目。我尝试了Visual Studio 2017 for Windows和VS for Mac 2019.它是一个内部/企业应用程序,它有任何问题......
钥匙扣电话:
KeyChain kc = new KeyChain("USER_DATA", "de.rlp.myCompany.Shared");
var data = kc.Find("LOGIN_DATA");
钥匙扣级:
public class KeyChain
{
public string ServiceName { get; set; }
public string GroupName { get; set; }
public KeyChain(string serviceName, string groupName = null)
{
ServiceName = serviceName;
GroupName = groupName;
}
public byte[] Find(string key)
{
SecStatusCode res;
var rec = PrepareDictionary(key);
var match = SecKeyChain.QueryAsRecord(rec, out res);
if (res == SecStatusCode.Success) // ItemNotFound return-code here
{
return match.ValueData.ToArray();
}
else
{
System.Diagnostics.Debug.Write(res.ToString());
}
return null;
}
private SecRecord PrepareDictionary(string key)
{
var sr = new SecRecord(SecKind.GenericPassword)
{
Service = this.ServiceName,
Generic = NSData.FromString (key),
Account = key,
Accessible = SecAccessible.AlwaysThisDeviceOnly
};
if (string.IsNullOrEmpty(GroupName))
{
sr.AccessGroup = GroupName;
}
return sr;
}
}
权利入境
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)de.rlp.myCompany.Shared</string>
</array>