Esempio n. 1
0
        private MemoryAlloc AllocateString(string value)
        {
            MemoryAlloc alloc = new MemoryAlloc((value.Length + 1) * 2);

            alloc.WriteUnicodeString(0, value);
            alloc.WriteInt16(value.Length * 2, 0);

            return(alloc);
        }
Esempio n. 2
0
        public static bool PromptForCredentials(
            IWin32Window parent,
            string messageText,
            string captionText,
            string targetName,
            Win32Error errorCode,
            ref string userName,
            ref string password,
            ref bool save,
            CredUiFlags flags
            )
        {
            const int maxBytes = 0x200;
            const int maxChars = (maxBytes - 2) / 2;

            Win32Error result;
            CredUiInfo info = new CredUiInfo();

            if (userName.Length > maxChars || password.Length > maxChars)
                throw new ArgumentException("The user name or password string is too long.");

            info.Size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CredUiInfo));
            info.Parent = parent != null ? parent.Handle : IntPtr.Zero;
            info.MessageText = messageText;
            info.CaptionText = captionText;

            using (var userNameAlloc = new MemoryAlloc(maxBytes))
            using (var passwordAlloc = new MemoryAlloc(maxBytes))
            {
                userNameAlloc.WriteUnicodeString(0, userName);
                userNameAlloc.WriteInt16(userName.Length * 2, 0);
                passwordAlloc.WriteUnicodeString(0, password);
                passwordAlloc.WriteInt16(password.Length * 2, 0);

                result = Win32.CredUIPromptForCredentials(
                    ref info,
                    targetName,
                    IntPtr.Zero,
                    errorCode,
                    userNameAlloc,
                    maxBytes / 2,
                    passwordAlloc,
                    maxBytes / 2,
                    ref save,
                    flags
                    );

                if (result == Win32Error.Cancelled)
                    return false;
                if (result != Win32Error.Success)
                    Win32.Throw(result);

                userName = userNameAlloc.ReadUnicodeString(0);
                password = passwordAlloc.ReadUnicodeString(0);

                return true;
            }
        }
Esempio n. 3
0
        public static bool PromptForCredentials(
            IWin32Window parent,
            string messageText,
            string captionText,
            string targetName,
            Win32Error errorCode,
            ref string userName,
            ref string password,
            ref bool save,
            CredUiFlags flags
            )
        {
            const int maxBytes = 0x200;
            const int maxChars = (maxBytes - 2) / 2;

            Win32Error result;
            CredUiInfo info = new CredUiInfo();

            if (userName.Length > maxChars || password.Length > maxChars)
            {
                throw new ArgumentException("The user name or password string is too long.");
            }

            info.Size        = CredUiInfo.SizeOf;
            info.Parent      = parent != null ? parent.Handle : IntPtr.Zero;
            info.MessageText = messageText;
            info.CaptionText = captionText;

            using (MemoryAlloc userNameAlloc = new MemoryAlloc(maxBytes))
                using (MemoryAlloc passwordAlloc = new MemoryAlloc(maxBytes))
                {
                    userNameAlloc.WriteUnicodeString(0, userName);
                    userNameAlloc.WriteInt16(userName.Length * 2, 0);
                    passwordAlloc.WriteUnicodeString(0, password);
                    passwordAlloc.WriteInt16(password.Length * 2, 0);

                    result = Win32.CredUIPromptForCredentials(
                        ref info,
                        targetName,
                        IntPtr.Zero,
                        errorCode,
                        userNameAlloc,
                        maxBytes / 2,
                        passwordAlloc,
                        maxBytes / 2,
                        ref save,
                        flags
                        );

                    if (result == Win32Error.Cancelled)
                    {
                        return(false);
                    }
                    if (result != Win32Error.Success)
                    {
                        Win32.Throw(result);
                    }

                    userName = userNameAlloc.ReadUnicodeString(0);
                    password = passwordAlloc.ReadUnicodeString(0);

                    return(true);
                }
        }