コード例 #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);
        }
コード例 #2
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);
 }