Exemplo n.º 1
0
        public static bool TryWrite(CredentialInput credentialInput)
        {
            //    unmanagedCodePermission.Demand();

            if (string.IsNullOrEmpty(credentialInput.TargetName))
            {
                throw new InvalidOperationException("Target must be specified.");
            }

            if (credentialInput.Comment != null && credentialInput.Comment.Length > NativeMethods.CRED_MAX_STRING_LENGTH)
            {
                throw new ArgumentOutOfRangeException(
                          $"The comment has exceeded {NativeMethods.CRED_MAX_STRING_LENGTH} characters.");
            }

            if (credentialInput.UserName.Length > NativeMethods.CRED_MAX_USERNAME_LENGTH)
            {
                throw new ArgumentOutOfRangeException(
                          $"The UserName has exceeded {NativeMethods.CRED_MAX_USERNAME_LENGTH} characters.");
            }

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

            NativeMethods.CREDENTIAL parameters;
            IntPtr credentialBlob = IntPtr.Zero;

            try
            {
                parameters = new NativeMethods.CREDENTIAL
                {
                    Flags      = credentialInput.Flags,
                    Type       = (int)credentialInput.CredentialType,
                    TargetName = credentialInput.TargetName,
                    Comment    = credentialInput.Comment,
                    // LastWritten = // dont' work on write.
                    CredentialBlobSize = bytes.Length,
                    CredentialBlob     = Marshal.StringToCoTaskMemUni(credentialInput.Password),
                    Persist            = (int)credentialInput.PersistanceType,
                    //AttributeCount = 0, // attributes is not used.
                    // TargetAlias is not used.
                    UserName = credentialInput.UserName,
                };
                credentialBlob = parameters.CredentialBlob;
                if (!NativeMethods.CredWrite(ref parameters, 0U))
                {
                    return(false);
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(credentialBlob);
            }
            return(true);
        }
Exemplo n.º 2
0
 static CredentialOutput LoadInternal(NativeMethods.CREDENTIAL credential, string targetName)
 => new CredentialOutput(
     credentialType: (CredentialType)credential.Type,
     targetName: targetName,
     comment: credential.Comment,
     DateTime.FromFileTimeUtc(credential.LastWritten),
     credential.CredentialBlobSize > 0
                                 ? Marshal.PtrToStringUni(credential.CredentialBlob, credential.CredentialBlobSize / 2)
                                 : null,
     (PersistanceType)credential.Persist,
     credential.UserName
     );
Exemplo n.º 3
0
 internal static extern bool CredWrite([In] ref NativeMethods.CREDENTIAL userCredential, [In] uint flags);