Exemplo n.º 1
0
        public bool Save()
        {
            CheckNotDisposed();
            _unmanagedCodePermission.Demand();

            byte[] passwordBytes = Encoding.Unicode.GetBytes(Password);
            if (Password.Length > (512))
            {
                throw new ArgumentOutOfRangeException("The password has exceeded 512 bytes.");
            }

            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);
        }
Exemplo n.º 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);
 }