Esempio n. 1
0
        /// <summary>
        /// Deletes this instance.
        /// </summary>
        /// <returns><c>true</c> if credential was deleted properly, <c>false</c> otherwise.</returns>
        /// <exception cref="System.InvalidOperationException">Target must be specified to delete a credential.</exception>
        public bool Delete()
        {
            CheckNotDisposed();
            UnmanagedCodePermission.Demand();

            if (string.IsNullOrEmpty(Target))
            {
                throw new InvalidOperationException("Target must be specified to delete a credential.");
            }

            return(NativeCode.CredDelete(Target, Type, 0));
        }
Esempio n. 2
0
        /// <summary>
        /// Remove stored credentials from windows credential store
        /// </summary>
        /// <param name="Target">Name of the application/Url where the credential is used for</param>
        /// <returns>True: Success, False: Failure</returns>
        public static bool RemoveCredentials(string Target, CredentialType type = CredentialType.Generic)
        {
            IntPtr credPointer;
            var    result = NativeCode.CredRead(Target, type, 0, out credPointer);

            if (!result)
            {
                return(false);
            }

            // Make the API call using the P/Invoke signature
            var ret       = NativeCode.CredDelete(Target, type, 0);
            int lastError = Marshal.GetLastWin32Error();

            if (!ret)
            {
                throw new Win32Exception(lastError, "CredDelete throw an error");
            }
            return(ret);
        }