public IEnumerable <string> GetUsernames(string host)
        {
            if (this._objectAlreadyDisposed)
            {
                throw new ObjectDisposedException("CredentialStore");
            }
            List <string> list = new List <string>();

            if (File.Exists(this._credentialFilePath))
            {
                FileStream fileStream = (FileStream)null;
                try
                {
                    fileStream = this.OpenFile(FileShare.Read);
                    foreach (XmlNode node in this.LoadCredentialsDocument((Stream)fileStream).SelectNodes("/Credentials/passwordEntry"))
                    {
                        if (CredentialStore.IsValidPasswordEntryNode(node) && node["host"].InnerText.Equals(host, StringComparison.OrdinalIgnoreCase))
                        {
                            list.Add(node["username"].InnerText);
                        }
                    }
                }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Dispose();
                    }
                }
            }
            return((IEnumerable <string>)list);
        }
        public void ClearPasswords()
        {
            if (this._objectAlreadyDisposed)
            {
                throw new ObjectDisposedException("CredentialStore");
            }
            if (!File.Exists(this._credentialFilePath))
            {
                return;
            }
            FileStream fileStream = (FileStream)null;

            try
            {
                fileStream = this.OpenFile(FileShare.None);
                XmlDocument credentialsXmlDocument = this.LoadCredentialsDocument((Stream)fileStream);
                XmlNode     xmlNode = credentialsXmlDocument.SelectSingleNode("/Credentials");
                foreach (XmlNode oldChild in credentialsXmlDocument.SelectNodes("/Credentials/passwordEntry"))
                {
                    xmlNode.RemoveChild(oldChild);
                }
                CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, (Stream)fileStream);
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }
        }
        public bool RemovePassword(string host, string username)
        {
            if (this._objectAlreadyDisposed)
            {
                throw new ObjectDisposedException("CredentialStore");
            }
            bool flag = false;

            if (File.Exists(this._credentialFilePath))
            {
                FileStream fileStream = (FileStream)null;
                try
                {
                    fileStream = this.OpenFile(FileShare.None);
                    XmlDocument credentialsXmlDocument = this.LoadCredentialsDocument((Stream)fileStream);
                    XmlNode     credentialNode         = CredentialStore.GetCredentialNode(credentialsXmlDocument, host, username, this._isUsernameCaseSensitive);
                    flag = credentialNode != null;
                    if (credentialNode != null)
                    {
                        credentialsXmlDocument.SelectSingleNode("/Credentials").RemoveChild(credentialNode);
                        CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, (Stream)fileStream);
                    }
                }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Dispose();
                    }
                }
            }
            return(flag);
        }
        public bool AddPassword(string host, string username, char[] password)
        {
            if (this._objectAlreadyDisposed)
            {
                throw new ObjectDisposedException("CredentialStore");
            }
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentException("Host name cannot be empty.", "host");
            }
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("User name cannot be empty.", "username");
            }
            if (password == null)
            {
                password = new char[0];
            }
            FileStream fileStream = (FileStream)null;
            bool       flag;

            try
            {
                if (!File.Exists(this._credentialFilePath))
                {
                    using (File.Create(this._credentialFilePath, 8192, FileOptions.RandomAccess, CredentialStore.GetSecuritySettings()))
                        ;
                    fileStream = this.OpenFile(FileShare.None);
                    CredentialStore.InitializeCredentialsDocument((Stream)fileStream);
                }
                else
                {
                    fileStream = this.OpenFile(FileShare.None);
                }
                XmlDocument credentialsXmlDocument = this.LoadCredentialsDocument((Stream)fileStream);
                XmlNode     xmlNode = CredentialStore.GetCredentialNode(credentialsXmlDocument, host, username, this._isUsernameCaseSensitive);
                flag = xmlNode == null;
                if (xmlNode == null)
                {
                    xmlNode = (XmlNode)credentialsXmlDocument.CreateElement("passwordEntry");
                }
                else
                {
                    xmlNode.RemoveAll();
                }
                CredentialStore.FillCredentialNode(xmlNode, host, username, password);
                Array.Clear((Array)password, 0, password.Length);
                credentialsXmlDocument.SelectSingleNode("/Credentials").AppendChild(xmlNode);
                CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, (Stream)fileStream);
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }
            return(flag);
        }
        private static char[] GetPasswordInternal(XmlDocument credentialsXmlDocument, string host, string username, bool isUsernameCaseSensitive)
        {
            char[]  chArray        = (char[])null;
            XmlNode credentialNode = CredentialStore.GetCredentialNode(credentialsXmlDocument, host, username, isUsernameCaseSensitive);

            if (credentialNode != null)
            {
                chArray = CredentialStore.DecryptPassword(credentialNode["password"].InnerText);
            }
            return(chArray);
        }
        private static void InitializeCredentialsDocument(Stream credentialsFile)
        {
            XmlDocument credentialsXmlDocument = new XmlDocument();

            credentialsXmlDocument.AppendChild((XmlNode)credentialsXmlDocument.CreateXmlDeclaration("1.0", "UTF-8", (string)null));
            XmlElement element1 = credentialsXmlDocument.CreateElement("Credentials");
            XmlElement element2 = credentialsXmlDocument.CreateElement("version");

            element2.InnerText = "2.0";
            element1.AppendChild((XmlNode)element2);
            credentialsXmlDocument.AppendChild((XmlNode)element1);
            CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, credentialsFile);
        }
        private static void FillCredentialNode(XmlNode element, string host, string username, char[] password)
        {
            XmlElement element1 = element.OwnerDocument.CreateElement("host");

            element1.InnerText = host;
            element.AppendChild((XmlNode)element1);
            XmlElement element2 = element.OwnerDocument.CreateElement("username");

            element2.InnerText = username;
            element.AppendChild((XmlNode)element2);
            XmlElement element3 = element.OwnerDocument.CreateElement("password");

            element3.InnerText = CredentialStore.EncryptPassword(password);
            element.AppendChild((XmlNode)element3);
        }
        private static char[] UnobfuscatePassword(string password, string host, string username)
        {
            byte[] array = Convert.FromBase64String(password);
            byte   num   = (byte)(CredentialStore.GetHashCode(host.ToLower() + username) % 256);

            for (int index = 0; index < array.Length; ++index)
            {
                array[index] ^= num;
            }
            int length = Array.IndexOf <byte>(array, (byte)0);

            if (length < 0)
            {
                throw new FormatException("Invalid password format. " + string.Format("Host: {0}, Username: {1}", (object)host, (object)username));
            }
            byte[] bytes = new byte[length];
            Array.Copy((Array)array, (Array)bytes, bytes.Length);
            return(Encoding.UTF8.GetChars(bytes));
        }
        private static XmlNode GetCredentialNode(XmlDocument credentialsXmlDocument, string host, string username, bool isUsernameCaseSensitive)
        {
            XmlNode xmlNode = (XmlNode)null;

            foreach (XmlNode node in credentialsXmlDocument.SelectNodes("/Credentials/passwordEntry"))
            {
                if (CredentialStore.IsValidPasswordEntryNode(node) && node["host"].InnerText.Equals(host, StringComparison.OrdinalIgnoreCase))
                {
                    string           innerText      = node["username"].InnerText;
                    StringComparison comparisonType = isUsernameCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
                    if (string.Equals(innerText, username, comparisonType))
                    {
                        xmlNode = node;
                        break;
                    }
                }
            }
            return(xmlNode);
        }
Example #10
0
        public IEnumerable <string> GetHosts()
        {
            if (this._objectAlreadyDisposed)
            {
                throw new ObjectDisposedException("CredentialStore");
            }
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            if (File.Exists(this._credentialFilePath))
            {
                FileStream fileStream = (FileStream)null;
                try
                {
                    fileStream = this.OpenFile(FileShare.Read);
                    foreach (XmlNode node in this.LoadCredentialsDocument((Stream)fileStream).SelectNodes("/Credentials/passwordEntry"))
                    {
                        if (CredentialStore.IsValidPasswordEntryNode(node))
                        {
                            string innerText = node["host"].InnerText;
                            string key       = innerText.ToLower();
                            if (!dictionary.ContainsKey(key))
                            {
                                dictionary[key] = innerText;
                            }
                        }
                    }
                }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Dispose();
                    }
                }
            }
            return((IEnumerable <string>)dictionary.Values);
        }