Exemple #1
0
 internal extern static CredUIReturnCodes CredUIPromptForCredentialsX(
     ref CREDUI_INFO creditUR,
     string targetName,
     IntPtr reserved1,
     int iError,
     StringBuilder userName,
     int maxUserName,
     StringBuilder password,
     int maxPassword,
     ref int iSave,
     CredUiFlags flags);
        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;
            }
        }
        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);
                }
        }