Microsoft.Alm.Authentication.NativeMethods.CredWrite C# (CSharp) Method

CredWrite() private method

private CredWrite ( Credential &credential, UInt32 flags ) : bool
credential Credential
flags System.UInt32
return bool
        internal static extern bool CredWrite(ref Credential credential, UInt32 flags);

Usage Example

        protected void WriteCredential(string targetName, Credential credentials)
        {
            NativeMethods.Credential credential = new NativeMethods.Credential()
            {
                Type               = NativeMethods.CredentialType.Generic,
                TargetName         = targetName,
                CredentialBlob     = Marshal.StringToCoTaskMemUni(credentials.Password),
                CredentialBlobSize = (uint)Encoding.Unicode.GetByteCount(credentials.Password),
                Persist            = NativeMethods.CredentialPersist.LocalMachine,
                AttributeCount     = 0,
                UserName           = credentials.Username,
            };
            try
            {
                if (!NativeMethods.CredWrite(ref credential, 0))
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    throw new Exception("Failed to write credentials", new Win32Exception(errorCode));
                }

                Git.Trace.WriteLine($"credentials for '{targetName}' written to store.");
            }
            finally
            {
                if (credential.CredentialBlob != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(credential.CredentialBlob);
                }
            }
        }
All Usage Examples Of Microsoft.Alm.Authentication.NativeMethods::CredWrite