/// <summary>
        /// Check if the entry exists. Ignore for now the passoword.
        /// </summary>
        public bool Exist(string targetName, string userName, string password, WindowsCredentialTypes.CredentialType type)
        {
            IntPtr credPtr;

            if (CredRead(targetName, type, 0, out credPtr))
            {
                WindowsCredentialTypes.Credential cred = (WindowsCredentialTypes.Credential)MarshalNativeToManaged(credPtr);

                return(cred.TargetName.Equals(targetName, StringComparison.CurrentCultureIgnoreCase) &&
                       (cred.UserName.Equals(userName, StringComparison.CurrentCultureIgnoreCase) ||
                        userName.IsNullOrEmpty()));
            }
            else
            {
                return(false);
            }
        }
        public bool Add(string targetName, string userName, string password, WindowsCredentialTypes.CredentialType type, WindowsCredentialTypes.CredentialPersist persist)
        {
            // Check if the password is not too long.
            byte[] byteArray = Encoding.Unicode.GetBytes(password);

            if (byteArray.Length > 512)
            {
                throw new ArgumentOutOfRangeException("The password has exceeded 512 bytes.");
            }

            WindowsCredentialTypes.Credential cred = new WindowsCredentialTypes.Credential()
            {
                UserName           = userName,
                TargetName         = targetName,
                TargetAlias        = null,
                Persist            = persist,
                Comment            = null,
                Flags              = 0,
                Type               = type,
                CredentialBlob     = password,
                CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(password).Length,
                Attributes         = null,
                AttributeCount     = 0
            };

            WindowsCredentialTypes.NativeCredential ncred = GetNativeCredential(cred);

            // Write the info into the CredMan storage.
            bool written   = CredWrite(ref ncred, 0);
            int  lastError = Marshal.GetLastWin32Error();

            Marshal.FreeCoTaskMem(ncred.TargetName);
            Marshal.FreeCoTaskMem(ncred.CredentialBlob);
            Marshal.FreeCoTaskMem(ncred.UserName);

            if (written)
            {
                return(true);
            }
            else
            {
                string message = string.Format("CredWrite failed with the error code {0}.", lastError);
                throw new Exception(message);
            }
        }
 static extern bool CredDelete(string targetName, WindowsCredentialTypes.CredentialType type, UInt32 flags);
 static extern bool CredRead(string target, WindowsCredentialTypes.CredentialType type, int reservedFlag, out IntPtr credentialPtr);
 public bool Delete(string targetName, WindowsCredentialTypes.CredentialType type)
 {
     return(CredDelete(targetName, type, 0));
 }
 public bool Exist(string targetName, WindowsCredentialTypes.CredentialType type)
 {
     return(Exist(targetName, null, null, type));
 }