public override string this [string key] { get { byte[] pw; var status = SecKeyChain.FindGenericPassword(ApplicationName, key, out pw); switch (status) { case SecStatusCode.Success: return(Encoding.UTF8.GetString(pw)); case SecStatusCode.ItemNotFound: return(null); default: status.AndThrowExceptionOnFailure(); return(null); } } set { if (Contains(key)) { Remove(key); } Add(key, value); } }
public string GetToken() { byte[] password_bytes; SecStatusCode result = SecKeyChain.FindGenericPassword( SERVICE, ACCOUNT, out password_bytes); if (result != SecStatusCode.Success) { throw new Exception("Could not find token in Keychain"); } return(Encoding.UTF8.GetString(password_bytes)); }
public static string GetCredentialFromFromKeychain(string username) { byte[] passwordBytes; if (SecKeyChain.FindGenericPassword(ServiceName, username, out passwordBytes) != SecStatusCode.Success) { return(null); } if (passwordBytes == null || passwordBytes.Length == 0) { return(null); } return(Encoding.UTF8.GetString(passwordBytes)); }
public override bool Contains(string key) { byte[] pw; var status = SecKeyChain.FindGenericPassword(ApplicationName, key, out pw); switch (status) { case SecStatusCode.Success: return(true); case SecStatusCode.ItemNotFound: return(false); default: status.AndThrowExceptionOnFailure(); return(false); } }
public static void SaveCredentialToKeychain(string username, string password) { SecStatusCode code = SecStatusCode.NotAvailable; byte[] passwordBytes; code = SecKeyChain.FindGenericPassword(ServiceName, username, out passwordBytes); if (code == SecStatusCode.Success) { code = SecKeyChain.Update(new SecRecord(SecKind.GenericPassword) { Service = ServiceName, Account = username }, new SecRecord(SecKind.GenericPassword) { Account = username, ValueData = NSData.FromString(password) }); } else { code = SecKeyChain.AddGenericPassword(ServiceName, username, System.Text.Encoding.UTF8.GetBytes(password)); } }