Ejemplo n.º 1
0
        void Protocol(SecProtocol protocol)
        {
            var rec = new SecRecord(SecKind.InternetPassword)
            {
                Account = "Protocol"
            };

            SecKeyChain.Remove(rec);              // it might already exists (or not)

            rec = new SecRecord(SecKind.InternetPassword)
            {
                Account   = "Protocol",
                ValueData = NSData.FromString("Password"),
                Protocol  = protocol,
                Server    = "www.xamarin.com"
            };

            Assert.That(SecKeyChain.Add(rec), Is.EqualTo(SecStatusCode.Success), "Add");

            SecStatusCode code;
            var           match = SecKeyChain.QueryAsRecord(rec, out code);

            Assert.That(code, Is.EqualTo(SecStatusCode.Success), "QueryAsRecord");

            Assert.That(match.Protocol, Is.EqualTo(protocol), "Protocol");
        }
Ejemplo n.º 2
0
        static SecRecord SecKeychainFindInternetPassword(Uri uri, SecProtocol protocol, out SecRecord searchRecord)
        {
            // Look for an internet password for the given protocol and auth mechanism
            searchRecord = uri.ToSecRecord();
            if (protocol != SecProtocol.Invalid)
            {
                searchRecord.Protocol = protocol;
            }

            var data = SecKeyChain.QueryAsRecord(searchRecord, out SecStatusCode code);

            if (code == SecStatusCode.ItemNotFound)
            {
                // Fall back to looking for a password without use SecProtocol && SecAuthenticationType
                searchRecord.Protocol           = SecProtocol.Http;       // Http is the default used by SecKeyChain internally
                searchRecord.AuthenticationType = SecAuthenticationType.Default;

                data = SecKeyChain.QueryAsRecord(searchRecord, out code);
            }

            if (code != SecStatusCode.Success)
            {
                return(null);
            }

            return(data);
        }
Ejemplo n.º 3
0
        void Protocol(SecProtocol protocol)
        {
            var rec = new SecRecord(SecKind.InternetPassword)
            {
                Account = $"Protocol-{protocol}-{CFBundle.GetMain ().Identifier}-{GetType ().FullName}-{Process.GetCurrentProcess ().Id}",
            };

            try {
                SecKeyChain.Remove(rec);                  // it might already exists (or not)

                rec = new SecRecord(SecKind.InternetPassword)
                {
                    Account   = "Protocol",
                    ValueData = NSData.FromString("Password"),
                    Protocol  = protocol,
                    Server    = "www.xamarin.com"
                };

                Assert.That(SecKeyChain.Add(rec), Is.EqualTo(SecStatusCode.Success), "Add");

                SecStatusCode code;
                var           match = SecKeyChain.QueryAsRecord(rec, out code);
                Assert.That(code, Is.EqualTo(SecStatusCode.Success), "QueryAsRecord");

                Assert.That(match.Protocol, Is.EqualTo(protocol), "Protocol");
            } finally {
                // Clean up after us
                SecKeyChain.Remove(rec);
            }
        }
Ejemplo n.º 4
0
        public static Tuple <string, string> FindInternetUserNameAndPassword(Uri url, SecProtocol protocol)
        {
            var record = SecKeychainFindInternetPassword(url, protocol, out _);

            if (record != null)
            {
                string username = record.Account != null?NSString.FromData(record.Account, NSStringEncoding.UTF8) : null;

                string password = record.ValueData != null?NSString.FromData(record.ValueData, NSStringEncoding.UTF8) : null;

                return(Tuple.Create(username, password));
            }
            return(null);
        }