public static MemoryRegion PackCredentials(CredPackFlags flags, string userName, string password)
        {
            MemoryAlloc data = new MemoryAlloc(0x100);
            int         size = data.Size;

            if (string.IsNullOrEmpty(userName))
            {
                userName = string.Empty;
            }
            if (string.IsNullOrEmpty(password))
            {
                password = string.Empty;
            }

            if (!Win32.CredPackAuthenticationBuffer(flags, userName, password, data, ref size))
            {
                data.ResizeNew(size);

                if (!Win32.CredPackAuthenticationBuffer(flags, userName, password, data, ref size))
                {
                    Win32.Throw();
                }
            }

            return(data);
        }
 public static extern bool CredUnPackAuthenticationBuffer(CredPackFlags dwFlags,
                                                          IntPtr pAuthBuffer,
                                                          uint cbAuthBuffer,
                                                          StringBuilder pszUserName,
                                                          ref int pcchMaxUserName,
                                                          StringBuilder pszDomainName,
                                                          ref int pcchMaxDomainame,
                                                          StringBuilder pszPassword,
                                                          ref int pcchMaxPassword);
        public static void UnpackCredentials(
            MemoryRegion buffer,
            CredPackFlags flags,
            out string domainName,
            out string userName,
            out string password
            )
        {
            using (MemoryAlloc domainNameBuffer = new MemoryAlloc(0x100))
                using (MemoryAlloc userNameBuffer = new MemoryAlloc(0x100))
                    using (MemoryAlloc passwordBuffer = new MemoryAlloc(0x100))
                    {
                        int domainNameSize = domainNameBuffer.Size / 2 - 1;
                        int userNameSize   = userNameBuffer.Size / 2 - 1;
                        int passwordSize   = passwordBuffer.Size / 2 - 1;

                        if (!Win32.CredUnPackAuthenticationBuffer(
                                flags,
                                buffer,
                                buffer.Size,
                                userNameBuffer,
                                ref userNameSize,
                                domainNameBuffer,
                                ref domainNameSize,
                                passwordBuffer,
                                ref passwordSize
                                ))
                        {
                            domainNameBuffer.ResizeNew(domainNameSize * 2 + 2);
                            userNameBuffer.ResizeNew(userNameSize * 2 + 2);
                            passwordBuffer.ResizeNew(passwordSize * 2 + 2);

                            if (!Win32.CredUnPackAuthenticationBuffer(
                                    flags,
                                    buffer,
                                    buffer.Size,
                                    userNameBuffer,
                                    ref userNameSize,
                                    domainNameBuffer,
                                    ref domainNameSize,
                                    passwordBuffer,
                                    ref passwordSize
                                    ))
                            {
                                Win32.Throw();
                            }
                        }

                        domainName = domainNameBuffer.ReadUnicodeString(0);
                        userName   = userNameBuffer.ReadUnicodeString(0);
                        password   = passwordBuffer.ReadUnicodeString(0);
                    }
        }
Beispiel #4
0
        public static void UnpackCredentials(
            MemoryRegion buffer,
            CredPackFlags flags,
            out string domainName,
            out string userName,
            out string password
            )
        {
            using (var domainNameBuffer = new MemoryAlloc(0x100))
            using (var userNameBuffer = new MemoryAlloc(0x100))
            using (var passwordBuffer = new MemoryAlloc(0x100))
            {
                int domainNameSize = domainNameBuffer.Size / 2 - 1;
                int userNameSize = userNameBuffer.Size / 2 - 1;
                int passwordSize = passwordBuffer.Size / 2 - 1;

                if (!Win32.CredUnPackAuthenticationBuffer(
                    flags,
                    buffer,
                    buffer.Size,
                    userNameBuffer,
                    ref userNameSize,
                    domainNameBuffer,
                    ref domainNameSize,
                    passwordBuffer,
                    ref passwordSize
                    ))
                {
                    domainNameBuffer.ResizeNew(domainNameSize * 2 + 2);
                    userNameBuffer.ResizeNew(userNameSize * 2 + 2);
                    passwordBuffer.ResizeNew(passwordSize * 2 + 2);

                    if (!Win32.CredUnPackAuthenticationBuffer(
                        flags,
                        buffer,
                        buffer.Size,
                        userNameBuffer,
                        ref userNameSize,
                        domainNameBuffer,
                        ref domainNameSize,
                        passwordBuffer,
                        ref passwordSize
                        ))
                        Win32.Throw();
                }

                domainName = domainNameBuffer.ReadUnicodeString(0);
                userName = userNameBuffer.ReadUnicodeString(0);
                password = passwordBuffer.ReadUnicodeString(0);
            }
        }
Beispiel #5
0
 private void Init(CredPackFlags flags, string pUserName, string pPassword)
 {
     if (!CredPackAuthenticationBuffer(flags, pUserName, pPassword, IntPtr.Zero, ref bufferSize) && Marshal.GetLastWin32Error() == 122)             /*ERROR_INSUFFICIENT_BUFFER*/
     {
         buffer = Marshal.AllocCoTaskMem(bufferSize);
         if (!CredPackAuthenticationBuffer(flags, pUserName, pPassword, buffer, ref bufferSize))
         {
             throw new Win32Exception();
         }
     }
     else
     {
         throw new Win32Exception();
     }
 }
        private void Init(CredPackFlags flags, string pUserName, string pPassword)
        {
            var bufferSize = 0;

            if (!CredPackAuthenticationBuffer(flags, pUserName, pPassword, IntPtr.Zero, ref bufferSize) && Win32Error.GetLastError() == Win32Error.ERROR_INSUFFICIENT_BUFFER)
            {
                buffer = new SafeCoTaskMemHandle(bufferSize);
                if (!CredPackAuthenticationBuffer(flags, pUserName, pPassword, DangerousHandle, ref bufferSize))
                {
                    throw new Win32Exception();
                }
            }
            else
            {
                throw new Win32Exception();
            }
        }
Beispiel #7
0
        public static MemoryRegion PackCredentials(CredPackFlags flags, string userName, string password)
        {
            MemoryAlloc data = new MemoryAlloc(0x100);
            int size = data.Size;

            if (userName == null)
                userName = "";
            if (password == null)
                password = "";

            if (!Win32.CredPackAuthenticationBuffer(flags, userName, password, data, ref size))
            {
                data.ResizeNew(size);

                if (!Win32.CredPackAuthenticationBuffer(flags, userName, password, data, ref size))
                    Win32.Throw();
            }

            return data;
        }
Beispiel #8
0
 public static extern bool CredPackAuthenticationBuffer(CredPackFlags dwFlags, IntPtr pszUserName, IntPtr pszPassword, IntPtr pPackedCredentials, ref int pcbPackedCredentials);