OGeek|极客世界-中国程序员成长平台

标题: c# - 跨应用钥匙串(keychain)访问,在哪里配置? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:41
标题: c# - 跨应用钥匙串(keychain)访问,在哪里配置?

我目前正在开发多个跨平台应用程序,这些应用程序(在 iOS 下)使用一些共享的钥匙串(keychain)条目。我目前的项目是在 android 上开始的,在我们有了一个工作版本之后,我继续在 iOS 版本上工作。我从早期项目中导入了我们的钥匙串(keychain)访问代码来访问我们共享的登录数据。只有这一次查询总是返回 SecStatusCode.ItemNotFound。

我比较了配置文件和权利,看起来都一样。过了一段时间,它让我发疯了,我创建了一个新的空应用程序,其中只有钥匙串(keychain)代码、相同的包标识符、配置文件和权利文件作为当前不工作的应用程序,它工作得很好并返回我的数据。

我的问题是,除了 entitlements.plist 和配置文件之外,还有什么地方需要配置,可能会干扰我对钥匙串(keychain)条目的访问?由于项目有点大,我不想将所有代码复制到新项目中。我尝试了适用于 Windows 的 Visual Studio 2017 和适用于 Mac 2019 的 VS。它是一个内部/企业应用程序,这是值得关注的......

钥匙串(keychain)调用:

KeyChain kc = new KeyChain("USER_DATA", "de.rlp.myCompany.Shared");
var data = kc.Find("LOGIN_DATA");

钥匙串(keychain)类:

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>



Best Answer-推荐答案


您是否已将两个应用程序从权利添加到同一个应用程序组/钥匙串(keychain)组并启用它。

VS 可能有问题。只需在苹果开发者网站上检查这两个应用程序的权利。 这可能是问题所在。

Documentation

关于c# - 跨应用钥匙串(keychain)访问,在哪里配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55628479/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4