Esempio n. 1
0
        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns><c>true</c> if credential is saved properly, <c>false</c> otherwise.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">password;The password has exceeded 512 bytes.</exception>
        public bool Save()
        {
            CheckNotDisposed();
            UnmanagedCodePermission.Demand();

            var passwordBytes = Encoding.Unicode.GetBytes(Password);

            if (Password.Length > (512))
            {
                throw new ArgumentOutOfRangeException("password", "The password has exceeded 512 bytes.");
            }

            var credential = new NativeCode.CREDENTIAL
            {
                TargetName         = Target,
                UserName           = Username,
                CredentialBlob     = Marshal.StringToCoTaskMemUni(Password),
                CredentialBlobSize = passwordBytes.Length,
                Comment            = Description,
                Type    = (int)Type,
                Persist = (int)PersistenceType
            };

            var result = NativeCode.CredWrite(ref credential, 0);

            if (!result)
            {
                return(false);
            }

            LastWriteTimeUtc = DateTime.UtcNow;
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the internal
        /// </summary>
        /// <param name="credential">The credential</param>
        internal void LoadInternal(NativeCode.CREDENTIAL credential)
        {
            Username = credential.UserName;

            if (credential.CredentialBlobSize > 0)
            {
                Password = Marshal.PtrToStringUni(credential.CredentialBlob, credential.CredentialBlobSize / 2);
            }

            Target           = credential.TargetName;
            Type             = (CredentialType)credential.Type;
            PersistenceType  = (PersistenceType)credential.Persist;
            Description      = credential.Comment;
            LastWriteTimeUtc = DateTime.FromFileTimeUtc(credential.LastWritten);
        }