internal static NativeCredential GetNativeCredential(Credential cred)
        {
            NativeCredential ncred = new NativeCredential();

            ncred.AttributeCount     = 0;
            ncred.AttributeCount     = 0;
            ncred.Attributes         = IntPtr.Zero;
            ncred.Comment            = IntPtr.Zero;
            ncred.TargetAlias        = IntPtr.Zero;
            ncred.Type               = cred.Type;
            ncred.Persist            = (UInt32)cred.Persist;
            ncred.CredentialBlobSize = (UInt32)cred.CredentialBlobSize;
            ncred.TargetName         = Marshal.StringToCoTaskMemUni(cred.TargetName);
            ncred.CredentialBlob     = Marshal.StringToCoTaskMemUni(cred.CredentialBlob);
            ncred.UserName           = Marshal.StringToCoTaskMemUni(cred.UserName);
            //                    var ncred = new NativeCredential
            //                    {
            //                        AttributeCount = 0,
            //                        Attributes = IntPtr.Zero,
            //                        Comment = IntPtr.Zero,
            //                        TargetAlias = IntPtr.Zero,
            //                        //Type = CRED_TYPE.DOMAIN_PASSWORD,
            //                        Type = cred.Type,
            //                        Persist = (UInt32)cred.Persist,
            //                        CredentialBlobSize = (UInt32)cred.CredentialBlobSize,
            //                        TargetName = Marshal.StringToCoTaskMemUni(cred.TargetName),
            //                        CredentialBlob = Marshal.StringToCoTaskMemUni(cred.CredentialBlob),
            //                        UserName = Marshal.StringToCoTaskMemUni(cred.UserName)
            //                    };
            return(ncred);
        }
        public static int WriteCred(string key, string userName, string secret, CRED_TYPE type, CRED_PERSIST credPersist)
        {
            var byteArray = Encoding.Unicode.GetBytes(secret);

            if (byteArray.Length > 512)
            {
                throw new ArgumentOutOfRangeException("The secret message has exceeded 512 bytes.");
            }
            var cred = new Credential
            {
                TargetName         = key,
                CredentialBlob     = secret,
                CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(secret).Length,
                AttributeCount     = 0,
                Attributes         = IntPtr.Zero,
                UserName           = userName,
                Comment            = null,
                TargetAlias        = null,
                Type    = type,
                Persist = credPersist
            };
            var ncred     = NativeCredential.GetNativeCredential(cred);
            var written   = CredWrite(ref ncred, 0);
            var lastError = Marshal.GetLastWin32Error();

            if (written)
            {
                return(0);
            }

            var message = "";

            if (lastError == 1312)
            {
                message = (string.Format("Failed to save " + key + " with error code {0}.", lastError)
                           + "  This error typically occurrs on home editions of Windows XP and Vista.  Verify the version of Windows is Pro/Business or higher.");
            }
            else
            {
                message = string.Format("Failed to save " + key + " with error code {0}.", lastError);
            }
            System.Diagnostics.Debug.WriteLine("Error:" + message);
            return(1);
        }
 //增加凭据
 static extern bool CredWrite([In] ref NativeCredential userCredential, [In] UInt32 flags);