4

Trying to set/get some passwords into Keychain on MacOS, using C#. Managed to add Xamarin.iOS assembly, but can't seem to go further. This is what I have,

using System;
using Security;
using Foundation;

var query = new SecRecord(SecKind.InternetPassword)
{
    Server = "bugzilla.novell.com",
    Account = "miguel"
};

But get an exception, which isn't that useful. I am not sure what value is null. This above example is straight from Xamarin website, but doesn't work. New to this Keychain business, so not sure what I am missing.

enter image description here

enter image description here

Can someone help, please? Any help is greatly appreciated!

I have also tried to instantiate without the enum, but still get the same error.

enter image description here

scorpion35
  • 790
  • 1
  • 8
  • 24

2 Answers2

2

Was able to solve this by calling security cmd - https://ss64.com/osx/security.html

But if anyone knows how this can be done via API and using C#, please let me know.

Dharman
  • 21,838
  • 18
  • 57
  • 107
scorpion35
  • 790
  • 1
  • 8
  • 24
2

You must properly initialize the Objective-C runtime as documented in Using Xamarin.Mac bindings for Console Apps before using Apple native APIs in C#:

NSApplication.Init();

Actually, calling ObjCRuntime.Runtime.EnsureInitialized(); would be enough but unlike NSApplication.Init(), the EnsureInitialized() method is internal and thus only callable through reflection, which is probably not a good idea.

0xced
  • 21,773
  • 10
  • 89
  • 238