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

            var FormatProvider = StringFormatProvider.GetProvider(Format);

            byte[] passwordBytes = FormatProvider.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     = FormatProvider.StringToCoTaskMem(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
        public bool Save()
        {
            var passwordBytes = Encoding.Unicode.GetBytes(Password);

            if (passwordBytes.Length > 512)
            {
                throw new InvalidOperationException("The password has exceeded 512 bytes.");
            }

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

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

            if (!result)
            {
                return(false);
            }
            LastWriteTimeUtc = DateTime.UtcNow;
            return(true);
        }
Exemplo n.º 3
0
 internal void LoadInternal(NativeMethods.CREDENTIAL credential)
 {
     Username = credential.UserName;
     if (credential.CredentialBlobSize > 0)
     {
         Password = StringFormatProvider.GetProvider(Format).PtrToString(credential.CredentialBlob, credential.CredentialBlobSize);
     }
     Target           = credential.TargetName;
     Type             = (CredentialType)credential.Type;
     PersistanceType  = (PersistanceType)credential.Persist;
     Description      = credential.Comment;
     LastWriteTimeUtc = DateTime.FromFileTimeUtc(credential.LastWritten);
 }
 private 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;
     PersistenceType  = (PersistenceType)credential.Persist;
     Description      = credential.Comment;
     LastWriteTimeUtc = DateTime.FromFileTimeUtc(credential.LastWritten);
 }
Exemplo n.º 5
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;
        }