Пример #1
0
        public bool Save()
        {
            CheckNotDisposed();

            byte[] passwordBytes = Encoding.Unicode.GetBytes(Password);
            if (Password.Length > (512))
            {
                throw new ArgumentOutOfRangeException(SR.CredentialsServicePasswordLengthExceeded);
            }

            NativeMethods.CREDENTIAL credential = new NativeMethods.CREDENTIAL();
            credential.TargetName         = Target;
            credential.UserName           = Username;
            credential.CredentialBlob     = Marshal.StringToCoTaskMemUni(Password);
            credential.CredentialBlobSize = passwordBytes.Length;
            credential.Comment            = Description;
            credential.Type    = (int)Type;
            credential.Persist = (int)PersistanceType;

            bool result = NativeMethods.CredWrite(ref credential, 0);

            if (!result)
            {
                return(false);
            }
            LastWriteTimeUtc = DateTime.UtcNow;
            return(true);
        }
Пример #2
0
 internal void LoadInternal(NativeMethods.CREDENTIAL credential)
 {
     Username = credential.UserName;
     if (credential.CredentialBlobSize > 0)
     {
         Password = Marshal.PtrToStringUni(credential.CredentialBlob, credential.CredentialBlobSize / 2);
     }
     Target           = credential.TargetName;
     Type             = (CredentialType)credential.Type;
     PersistanceType  = (PersistanceType)credential.Persist;
     Description      = credential.Comment;
     LastWriteTimeUtc = DateTime.FromFileTimeUtc(credential.LastWritten);
 }