protected Token ReadToken(string targetName)
        {
            if (targetName is null)
            {
                throw new ArgumentNullException(nameof(targetName));
            }

            try
            {
                if (Storage.TryReadSecureData(targetName, out string name, out byte[] data) &&
                    Token.GetTypeFromFriendlyName(name, out TokenType type) &&
                    Token.Deserialize(Context, data, type, out Token token))
                {
                    return(token);
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);

                Trace.WriteException(exception);
            }

            return(null);
        }
        protected Token ReadToken(string targetName)
        {
            Token  token   = null;
            IntPtr credPtr = IntPtr.Zero;

            try
            {
                if (NativeMethods.CredRead(targetName, NativeMethods.CredentialType.Generic, 0, out credPtr))
                {
                    NativeMethods.Credential credStruct = (NativeMethods.Credential)Marshal.PtrToStructure(credPtr, typeof(NativeMethods.Credential));
                    if (credStruct.CredentialBlob != null && credStruct.CredentialBlobSize > 0)
                    {
                        int    size  = (int)credStruct.CredentialBlobSize;
                        byte[] bytes = new byte[size];
                        Marshal.Copy(credStruct.CredentialBlob, bytes, 0, size);

                        TokenType type;
                        if (Token.GetTypeFromFriendlyName(credStruct.UserName, out type))
                        {
                            Token.Deserialize(bytes, type, out token);
                        }

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

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

                return(null);
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    NativeMethods.CredFree(credPtr);
                }
            }

            return(token);
        }
        protected IEnumerable <Secret> EnumerateCredentials(string @namespace)
        {
            foreach (var secure in Storage.EnumerateSecureData(@namespace))
            {
                if (Token.GetTypeFromFriendlyName(secure.Name, out TokenType type) &&
                    Token.Deserialize(Context, secure.Data, type, out Token token))
                {
                    yield return(token);
                }
                else
                {
                    var username = secure.Name;
                    var password = Unicode.GetString(secure.Data);

                    var credential = new Credential(username, password);

                    yield return(credential);
                }
            }
        }
        protected Token ReadToken(string targetName)
        {
            Trace.WriteLine("BaseSecureStore::ReadToken");

            Token  token   = null;
            IntPtr credPtr = IntPtr.Zero;

            try
            {
                if (!NativeMethods.CredRead(targetName, NativeMethods.CredentialType.Generic, 0, out credPtr))
                {
                    return(null);
                }

                NativeMethods.Credential credStruct = (NativeMethods.Credential)Marshal.PtrToStructure(credPtr, typeof(NativeMethods.Credential));
                if (credStruct.CredentialBlob == null || credStruct.CredentialBlobSize <= 0)
                {
                    return(null);
                }

                int    size  = (int)credStruct.CredentialBlobSize;
                byte[] bytes = new byte[size];
                Marshal.Copy(credStruct.CredentialBlob, bytes, 0, size);

                TokenType type;
                if (Token.GetTypeFromFriendlyName(credStruct.UserName, out type))
                {
                    Token.Deserialize(bytes, type, out token);
                }
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    NativeMethods.CredFree(credPtr);
                }
            }

            return(token);
        }