3

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

Andrew Nikolin
  • 297
  • 1
  • 4
  • 18

1 Answers1

6

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

If you are getting a -34018, then you need to enable KeyChain access in your Entitlements.plist:

enter image description here

And then make sure that the Entitlements.plist is assigned to your Custom Entitlements in your Project Build / iOS Bundle Signing:

enter image description here

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Wow, didn't know about Entitlements, strange that it worked before. Btw, I'm not sure how, but when yesterday I used Keychain.Net library everything started working again even though behind the curtains the library works almost the same way. Thanks anyway! – Andrew Nikolin Dec 27 '16 at 18:24
  • 1
    Another problem now is that I can't deploy my application to device after enabling KeyChain access "error MT1006: Could not install the application 'Myapp.app' on the device 'iPhone': Your code signing/provisioning profiles are not correctly configured. Probably you have an entitlement not supported by your current provisioning profile, or your device is not part of the current provisioning profile. Please check the iOS Device Log for details (error: 0xe8008016)." – Andrew Nikolin Dec 27 '16 at 18:30
  • @SushiHangover How do I add a keychain record to kSecAttrAccessGroupToken Access group? – Madhav Shenoy Sep 25 '17 at 02:25
  • @AndrewNikolin I'm facing the same error. Were you able to resolve this? – Madhav Shenoy Sep 26 '17 at 03:36
  • I am facing the same problem , do you have any solution for the problem ? – Jayasai Goutheman Oct 18 '18 at 07:44
  • Your provisioning profile within the Apple Developer portal has a list of entitlements it supports. You need to explicitly modify the entitlements for your profile to grant access to keychain groups. Login to the [Apple Developer portal](https://developer.apple.com/account/ios/profile/), in the left-hand menu under `Provisioning Profiles` select `All`, find your profile then edit it to include the necessary entitlement. – StoriKnow Mar 25 '19 at 21:41
  • Which entitlement should we add for allowing keychain access? Has anyone found solution for that? I am not able to deploy debug app to my device. – Manthan Feb 19 '20 at 11:03