CredWrite() private méthode

private CredWrite ( Credential &credential, UInt32 flags ) : bool
credential Credential
flags System.UInt32
Résultat bool
        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);
                }
            }
        }
        protected void WriteCredential(string targetName, Credential credentials)
        {
            Trace.WriteLine("BaseSecureStore::WriteCredential");

            NativeMethods.Credential credential = new NativeMethods.Credential()
            {
                Type           = NativeMethods.CredentialType.Generic,
                TargetName     = targetName,
                Persist        = NativeMethods.CredentialPersist.LocalMachine,
                AttributeCount = 0,
                UserName       = credentials.Username,
            };
            try
            {
                // https://msdn.microsoft.com/en-us/library/gg309393.aspx
                credential.CredentialBlob = Marshal.SecureStringToCoTaskMemUnicode(credentials.Password);
                // See calculation in http://referencesource.microsoft.com/#mscorlib/system/security/securestring.cs,eab10308ba549df3
                credential.CredentialBlobSize = (uint)(credentials.Password.Length + 1 /*null terminator*/) * 2 /*Unicode encoding*/;

                if (!NativeMethods.CredWrite(ref credential, 0))
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    Trace.WriteLine("BaseSecureStore::WriteCredential Failed to write credentials, error code " + errorCode);
                }
            }
            finally
            {
                if (credential.CredentialBlob != IntPtr.Zero)
                {
                    Marshal.ZeroFreeCoTaskMemUnicode(credential.CredentialBlob);
                }
            }
        }
        protected bool WriteToken(string targetName, Token token)
        {
            if (ReferenceEquals(targetName, null))
            {
                throw new ArgumentNullException(nameof(targetName));
            }
            if (ReferenceEquals(token, null))
            {
                throw new ArgumentNullException(nameof(token));
            }

            byte[] bytes = null;
            if (Token.Serialize(token, out bytes))
            {
                string name;
                if (Token.GetFriendlyNameFromType(token.Type, out name))
                {
                    NativeMethods.Credential credential = new NativeMethods.Credential()
                    {
                        Type               = NativeMethods.CredentialType.Generic,
                        TargetName         = targetName,
                        CredentialBlobSize = (uint)bytes.Length,
                        Persist            = NativeMethods.CredentialPersist.LocalMachine,
                        AttributeCount     = 0,
                        UserName           = name,
                    };
                    try
                    {
                        credential.CredentialBlob = Marshal.AllocCoTaskMem(bytes.Length);
                        Marshal.Copy(bytes, 0, credential.CredentialBlob, bytes.Length);

                        if (!NativeMethods.CredWrite(ref credential, 0))
                        {
                            int error = Marshal.GetLastWin32Error();
                            throw new Win32Exception(error, "Failed to write credentials");
                        }

                        Git.Trace.WriteLine($"token for '{targetName}' written to store.");
                    }
                    catch (Exception exception)
                    {
                        Debug.WriteLine(exception);

                        Git.Trace.WriteLine($"failed to write credentials: {exception.GetType().Name}.");

                        return(false);
                    }
                    finally
                    {
                        if (credential.CredentialBlob != IntPtr.Zero)
                        {
                            Marshal.FreeCoTaskMem(credential.CredentialBlob);
                        }
                    }
                }
            }

            return(true);
        }
        protected bool WriteCredential(string targetName, Credential credentials)
        {
            if (ReferenceEquals(targetName, null))
            {
                throw new ArgumentNullException(nameof(targetName));
            }
            if (ReferenceEquals(credentials, null))
            {
                throw new ArgumentNullException(nameof(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 error = Marshal.GetLastWin32Error();
                    throw new Win32Exception(error, "Failed to write credentials");
                }

                Git.Trace.WriteLine($"credentials for '{targetName}' written to store.");
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);

                Git.Trace.WriteLine($"failed to write credentials: {exception.GetType().Name}.");

                return(false);
            }
            finally
            {
                if (credential.CredentialBlob != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(credential.CredentialBlob);
                }
            }

            return(true);
        }
        protected void WriteToken(string targetName, Token token)
        {
            Trace.WriteLine("BaseSecureStore::WriteToken");

            byte[] bytes = null;
            if (!Token.Serialize(token, out bytes))
            {
                return;
            }

            string name;

            if (!Token.GetFriendlyNameFromType(token.Type, out name))
            {
                return;
            }

            NativeMethods.Credential credential = new NativeMethods.Credential()
            {
                Type               = NativeMethods.CredentialType.Generic,
                TargetName         = targetName,
                CredentialBlobSize = (uint)bytes.Length,
                Persist            = NativeMethods.CredentialPersist.LocalMachine,
                AttributeCount     = 0,
                UserName           = name,
            };
            try
            {
                credential.CredentialBlob = Marshal.AllocCoTaskMem(bytes.Length);
                Marshal.Copy(bytes, 0, credential.CredentialBlob, bytes.Length);

                if (!NativeMethods.CredWrite(ref credential, 0))
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    Trace.WriteLine("BaseSecureStore::WriteToken Failed to write credentials, error code " + errorCode);
                }
            }
            finally
            {
                if (credential.CredentialBlob != IntPtr.Zero)
                {
                    Marshal.ZeroFreeCoTaskMemUnicode(credential.CredentialBlob);
                }
            }
        }
        protected void WriteToken(string targetName, Token token)
        {
            byte[] bytes = null;
            if (Token.Serialize(token, out bytes))
            {
                string name;
                if (Token.GetFriendlyNameFromType(token.Type, out name))
                {
                    NativeMethods.Credential credential = new NativeMethods.Credential()
                    {
                        Type               = NativeMethods.CredentialType.Generic,
                        TargetName         = targetName,
                        CredentialBlobSize = (uint)bytes.Length,
                        Persist            = NativeMethods.CredentialPersist.LocalMachine,
                        AttributeCount     = 0,
                        UserName           = name,
                    };
                    try
                    {
                        credential.CredentialBlob = Marshal.AllocCoTaskMem(bytes.Length);
                        Marshal.Copy(bytes, 0, credential.CredentialBlob, bytes.Length);

                        if (!NativeMethods.CredWrite(ref credential, 0))
                        {
                            int errorCode = Marshal.GetLastWin32Error();
                            throw new Exception("Failed to write credentials", new Win32Exception(errorCode));
                        }

                        Git.Trace.WriteLine($"token for '{targetName}' written to store.");
                    }
                    finally
                    {
                        if (credential.CredentialBlob != IntPtr.Zero)
                        {
                            Marshal.FreeCoTaskMem(credential.CredentialBlob);
                        }
                    }
                }
            }
        }