Example #1
0
        /// <summary>
        /// Saves or updates the credential in the Credential Manager (CredMan).
        /// </summary>
        /// <param name="url">URL of site or tenant to save.</param>
        /// <param name="username">Username</param>
        /// <param name="password">Password</param>
        /// <param name="storageType">What type is stored?</param>
        /// <returns>Returns true if successful, otherwise false.</returns>
        public static bool SaveCredentials(string url, string username, string password, StorageType storageType)
        {
            bool       isSuccess  = false;
            Credential credential = null;

            try
            {
                // Save credentials
                credential = new Credential(username, password, GetTargetKey(url, storageType), CREDENTIAL_TYPE);
                credential.PersistanceType = PersistanceType.LocalComputer;
                credential.Description     = "Credentials stored by SharePoint Client Browser";

                isSuccess = credential.Save();
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error while trying to save credentials for {0}.", url), ex);
            }
            finally
            {
                if (credential != null)
                {
                    credential.Dispose();
                }
            }

            return(isSuccess);
        }
Example #2
0
        /// <summary>
        /// Deletes credential in Credential Manager (CredMan).
        /// </summary>
        /// <param name="url">URL of site or tenant to delete.</param>
        /// <param name="storageType">What type is stored?</param>
        /// <returns>Returns true if successful, otherwise false.</returns>
        public static bool DeleteCredentials(string url, StorageType storageType)
        {
            bool       isSuccess  = false;
            Credential credential = null;

            try
            {
                // Delete credentials
                credential = new Credential {
                    Target = GetTargetKey(url, storageType), Type = CREDENTIAL_TYPE
                };
                if (credential.Exists())
                {
                    isSuccess = credential.Delete();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error while trying to delete credentials for {0}.", url), ex);
            }
            finally
            {
                if (credential != null)
                {
                    credential.Dispose();
                }
            }

            return(isSuccess);
        }
Example #3
0
        /// <summary>
        /// Tries to retrieve the password from the Credential Manager (CredMan).
        /// </summary>
        /// <param name="url">URL of site or tenant related to the password.</param>
        /// <param name="password">Retrieved password when successfull.</param>
        /// <param name="storageType">What type is stored?</param>
        /// <returns>Returns true if successful, otherwise false.</returns>
        public static bool TryGetPassword(string url, out string password, StorageType storageType)
        {
            bool       isSuccess  = false;
            Credential credential = null;

            password = string.Empty;

            try
            {
                // Try to retrieve credentials
                credential = new Credential {
                    Target = GetTargetKey(url, storageType), Type = CREDENTIAL_TYPE
                };

                if (credential.Load())
                {
                    password  = credential.Password;
                    isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error while trying to retrieve credentials for {0}.", url), ex);
            }
            finally
            {
                if (credential != null)
                {
                    credential.Dispose();
                }
            }

            return(isSuccess);
        }
Example #4
0
        public Boolean UpdateProfile(String UserName, String Password, String Description = "")
        {
            Boolean result = false;

            try
            {
                Credential addCredential = new Credential(UserName, Password, UserName, CredentialType.Generic);
                addCredential.Description = Description;


                if (addCredential.Exists())
                {
                    addCredential.Delete();
                }

                if (!addCredential.Exists())
                {
                    addCredential.Save();
                }


                result = true;
                addCredential.Dispose();
            }
            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }
            return(result);
        }
Example #5
0
 public static void DeleteCredential(Credential credToDel)
 {
     if (null != credToDel)
     {
         credToDel.Delete();
         credToDel.Dispose();
     }
 }
        public void Credential_ShouldThrowObjectDisposedException()
        {
            Credential disposed = new Credential {
                Password = "******"
            };

            disposed.Dispose();
            disposed.Username = "******";
        }
        public void Credential_ShouldThrowObjectDisposedException()
        {
            Credential disposed = new Credential {
                Password = "******"
            };

            disposed.Dispose();
            Assert.Throws(typeof(ObjectDisposedException), () => disposed.Username = "******");
        }
        public int UnAdvise()
        {
            Credential.Dispose();
            if (Events != null)
            {
                Marshal.ReleaseComObject(Events);
                Credential.CredentialEvents = null;
                Events = null;
            }

            return(HRESULT.S_OK);
        }
        /// <summary>
        /// Add credentials.
        /// </summary>
        /// <param name="targetName">The target name.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="description">The description.</param>
        /// <param name="type">The credential type.</param>
        /// <param name="persistence">The credential persistence.</param>
        /// <returns>True if the credential was added; else false.</returns>
        public bool AddCredentials(string targetName, string username, string password,
                                   string description, CredentialType type, CredentialPersistence persistence)
        {
            Credential credential = null;

            try
            {
                // If the credentials already exist.
                if (Credential.Exists(targetName, type))
                {
                    throw new Exception("Credentials with the given target name and type already exist.");
                }

                // Construct the secure string password.
                SecureString passwordSecure = new SecureString();

                // Append the secure password for each character.
                foreach (char element in password)
                {
                    passwordSecure.AppendChar(element);
                }

                // Create the new credentials
                credential = new Credential(targetName, type, username, passwordSecure, persistence, description);

                // Save the credentials to the store.
                credential.Save();
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (credential != null)
                {
                    credential.Dispose();
                }
            }
        }
        /// <summary>
        /// Gets the credentials for the target name and type.
        /// </summary>
        /// <param name="targetName">The target name.</param>
        /// <param name="type">The credential type.</param>
        /// <returns>The credentials; else null.</returns>
        public Credential GetCredential(string targetName, CredentialType type)
        {
            Credential credential = null;

            try
            {
                // Load the credentials.
                credential = new Credential(targetName, type);
                credential.Load();
                return(credential);
            }
            catch (Exception)
            {
                if (credential != null)
                {
                    credential.Dispose();
                }

                throw;
            }
        }
Example #11
0
        public Boolean CheckProfileExists(String ProfileName)
        {
            Boolean result = false;

            try
            {
                Credential credential = new Credential();
                credential.Target = ProfileName;

                result = credential.Exists();

                credential.Dispose();
            }
            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }
            finally
            {
            }
            return(result);
        }
        /// <summary>
        /// Change the credential target name.
        /// </summary>
        /// <param name="targetName">The target name.</param>
        /// <param name="type">The credential type.</param>
        /// <param name="newTargetName">The new target name.</param>
        /// <returns>True if changed; else false.</returns>
        public bool ChangeCredentialTargetName(string targetName, CredentialType type, string newTargetName)
        {
            Credential credential = null;

            try
            {
                // Load the credentials.
                credential = new Credential(targetName, type);
                credential.ChangeTargetName(newTargetName);
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (credential != null)
                {
                    credential.Dispose();
                }
            }
        }
Example #13
0
        public CredentialInfo GetCredentials(String ProfileName)
        {
            CredentialInfo resultCredInfo = null;

            try
            {
                Credential credential = new Credential();
                credential.Target = ProfileName;

                if (credential.Load())
                {
                    resultCredInfo = new CredentialInfo(credential.Username, credential.Password, credential.Description, credential.Target, credential.LastWriteTimeUtc);
                }

                credential.Dispose();
            }
            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }

            return(resultCredInfo);
        }
 public void Credential_ShouldThrowObjectDisposedException()
 {
     Credential disposed = new Credential {Password = "******"};
     disposed.Dispose();
     disposed.Username = "******";
 }