菜鸟教程小白 发表于 2022-12-11 22:41:47

c# - 跨应用钥匙串(keychain)访问,在哪里配置?


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

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

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

<p>钥匙串(keychain)调用:</p>

<pre><code>KeyChain kc = new KeyChain(&#34;USER_DATA&#34;, &#34;de.rlp.myCompany.Shared&#34;);
var data = kc.Find(&#34;LOGIN_DATA&#34;);
</code></pre>

<p>钥匙串(keychain)类:</p>

<pre><code>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;
    }

}
</code></pre>

<p>权利-条目</p>

<pre><code>&lt;key&gt;keychain-access-groups&lt;/key&gt;
&lt;array&gt;
    &lt;string&gt;$(AppIdentifierPrefix)de.rlp.myCompany.Shared&lt;/string&gt;
&lt;/array&gt;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您是否已将两个应用程序从权利添加到同一个应用程序组/钥匙串(keychain)组并启用它。</p>

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

<p> <a href="https://developer.apple.com/documentation/security/keychain_services/keychain_items/sharing_access_to_keychain_items_among_a_collection_of_apps" rel="noreferrer noopener nofollow">Documentation</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于c# - 跨应用钥匙串(keychain)访问,在哪里配置?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55628479/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55628479/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - 跨应用钥匙串(keychain)访问,在哪里配置?