示例#1
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);
        }
示例#2
0
        /// <summary>
        /// Loads this instance.
        /// </summary>
        /// <returns><c>true</c> if credential is load properly, <c>false</c> otherwise.</returns>
        public bool Load()
        {
            CheckNotDisposed();
            UnmanagedCodePermission.Demand();

            IntPtr credPointer;

            var result = NativeCode.CredRead(Target, Type, 0, out credPointer);

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

            using (var credentialHandle = new NativeCode.CriticalCredentialHandle(credPointer))
            {
                LoadInternal(credentialHandle.GetCredential());
            }

            return(true);
        }