简体   繁体   中英

Cross-App Keychain Access, where to configure?

I am currently developing multiple cross-plattform apps which (under iOS) use some shared keychain entries. My current project started on android and after we had a working version I continued working on the iOS version. I imported our keychain access code from earlier projects to access our shared login-data. Only this time the queries always return SecStatusCode.ItemNotFound.

I compared the provisioning profile and the entitlements and all looks the same. After some time it drove me nuts and I created a new empty app with only the keychain-code, the same bundle identifier, provisioning profiles and entitlements file as the currently-not-working-app and it works just fine and returns my data.

To my question, where else are things to be configured, that could possibly interfere with my access to keychain entries besides entitlements.plist and provisioning profile? Since the project is somewhat bigger, I don't want to copy ALL code to fresh project. I tried both Visual Studio 2017 for Windows and VS for Mac 2019. It is an internal/enterprise app, it that is of any concern...

Keychain call:

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

Keychain-Class:

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

}

Entitlements-Entry

<key>keychain-access-groups</key>
<array>
    <string>$(AppIdentifierPrefix)de.rlp.myCompany.Shared</string>
</array>

Have you added both your applications to the same App Group/ Keychain Group from entitlements and enabled it.

VS might be buggy. Just check entitlements for both the apps on apple developer site. That might be the problem.

Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM