Exemplo n.º 1
0
        public static bool PromptForCredentials2(
            IWin32Window parent,
            string messageText,
            string captionText,
            Win32Error errorCode,
            ref string domainName,
            ref string userName,
            ref string password,
            ref bool save,
            CredUiWinFlags flags
            )
        {
            Win32Error result;
            CredUiInfo info = new CredUiInfo();
            int authenticationPackage = 0;
            IntPtr outAuthBuffer;
            int outAuthBufferSize;

            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 inAuthBuffer = PackCredentials(0, userName, password))
            {
                result = Win32.CredUIPromptForWindowsCredentials(
                    ref info,
                    errorCode,
                    ref authenticationPackage,
                    inAuthBuffer,
                    inAuthBuffer.Size,
                    out outAuthBuffer,
                    out outAuthBufferSize,
                    ref save,
                    flags
                    );

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

                try
                {
                    UnpackCredentials(
                        new MemoryRegion(outAuthBuffer, 0, outAuthBufferSize),
                        CredPackFlags.ProtectedCredentials,
                        out domainName,
                        out userName,
                        out password
                        );

                    return true;
                }
                finally
                {
                    System.Runtime.InteropServices.Marshal.FreeCoTaskMem(outAuthBuffer);
                }
            }
        }
Exemplo 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;
            }
        }