Ejemplo n.º 1
0
        public static void AddInternetPassword(Uri uri, string username, string password)
        {
            // See if there is already a password there for this uri
            var record = SecKeychainFindInternetPassword(uri, out SecRecord searchRecord);

            if (record == null)
            {
                record = uri.ToSecRecord(username, password);

                SecStatusCode result = SecKeyChain.Add(record);

                if (result != SecStatusCode.Success && result != SecStatusCode.DuplicateItem)
                {
                    throw new Exception("Could not add internet password to keychain: " + result.GetStatusDescription());
                }

                return;
            }

            // If there is, replace it with the new one
            var update = uri.ToSecRecord(username, password);

            SecKeyChain.Update(record, update);
        }
Ejemplo n.º 2
0
        static unsafe string GetUsernameFromKeychainItemRef(IntPtr itemRef)
        {
            int[] formatConstants = { (int)CssmDbAttributeFormat.String };
            int[] attributeTags   = { (int)SecItemAttr.Account };

            fixed(int *tags = attributeTags, formats = formatConstants)
            {
                var attributeInfo = new SecKeychainAttributeInfo {
                    Count  = 1,
                    Tag    = tags,
                    Format = formats
                };
                SecKeychainAttributeList *attributeList = null;
                SecItemClass itemClass = 0;

                try {
                    SecStatusCode status = SecKeychainItemCopyAttributesAndData(itemRef, &attributeInfo, ref itemClass, &attributeList, IntPtr.Zero, IntPtr.Zero);

                    if (status == SecStatusCode.ItemNotFound)
                    {
                        throw new Exception("Could not add internet password to keychain: " + status.GetStatusDescription());
                    }

                    if (status != SecStatusCode.Success)
                    {
                        throw new Exception("Could not find internet username and password: " + status.GetStatusDescription());
                    }

                    var userNameAttr = (SecKeychainAttribute *)attributeList->Attrs;

                    if (userNameAttr->Length == 0)
                    {
                        return(null);
                    }

                    return(Marshal.PtrToStringAuto(userNameAttr->Data, (int)userNameAttr->Length));
                } finally {
                    SecKeychainItemFreeAttributesAndData(attributeList, IntPtr.Zero);
                }
            }
        }
Ejemplo n.º 3
0
        public static void RemoveInternetUserNameAndPassword(Uri url)
        {
            var record = SecKeychainFindInternetPassword(url, out _);

            if (record != null)
            {
                SecStatusCode result = SecKeyChain.Remove(record);

                if (result != SecStatusCode.Success)
                {
                    throw new Exception("Could not delete internet password from keychain: " + result.GetStatusDescription());
                }
            }
        }