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 WriteToken(string targetName, Token token)
        {
            if (targetName is null)
            {
                throw new ArgumentNullException(nameof(targetName));
            }
            if (token is null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (Token.Serialize(Context, token, out byte[] data))
        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);
                        }
                    }
                }
            }
        }