예제 #1
0
 public static extern int CredUIPromptForWindowsCredentials(
     ref CredentialUiInfo credInfo,
     uint authError,
     ref uint authPackage,
     IntPtr inAuthBuffer,
     uint inAuthBufferSize,
     out IntPtr outAuthBuffer,
     out uint outAuthBufferSize,
     ref bool saveCredentials,
     CredentialUiWindowsFlags flags);
예제 #2
0
        public static Credential CredentialPrompt(Program program, TargetUri targetUri, string message)
        {
            if (program is null)
            {
                throw new ArgumentNullException(nameof(program));
            }
            if (targetUri is null)
            {
                throw new ArgumentNullException(nameof(targetUri));
            }
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var credUiInfo = new CredentialUiInfo
            {
                BannerArt   = IntPtr.Zero,
                CaptionText = program.Title,
                Parent      = program.ParentHwnd,
                MessageText = message,
                Size        = Marshal.SizeOf(typeof(CredentialUiInfo))
            };
            var    flags                = CredentialUiWindowsFlags.Generic;
            var    authPackage          = CredentialPackFlags.None;
            var    packedAuthBufferPtr  = IntPtr.Zero;
            var    inBufferPtr          = IntPtr.Zero;
            uint   packedAuthBufferSize = 0;
            bool   saveCredentials      = false;
            int    inBufferSize         = 0;
            string username;
            string password;

            if (program.ModalPromptDisplayDialog(ref credUiInfo,
                                                 ref authPackage,
                                                 packedAuthBufferPtr,
                                                 packedAuthBufferSize,
                                                 inBufferPtr,
                                                 inBufferSize,
                                                 saveCredentials,
                                                 flags,
                                                 out username,
                                                 out password))
            {
                return(new Credential(username, password));
            }

            return(null);
        }
 public static extern int CredUIPromptForWindowsCredentials(ref CredentialUiInfo credInfo, uint authError, ref CredentialPackFlags authPackage, IntPtr inAuthBuffer, uint inAuthBufferSize, out IntPtr outAuthBuffer, out uint outAuthBufferSize, ref bool saveCredentials, CredentialUiWindowsFlags flags);
 public static extern CredentialUiResult CredUIPromptForCredentials(ref CredentialUiInfo credUiInfo, string targetName, IntPtr reserved, uint authError, StringBuilder username, int usernameMaxLen, StringBuilder password, int passwordMaxLen, ref bool saveCredentials, CredentialUiFlags flags);
		internal static extern WindowsCredentialPromptReturnCode CredUIPromptForWindowsCredentials (ref CredentialUiInfo uiInfo,
			int authError, ref int authPackage, IntPtr inAuthBuffer, uint inAuthBufferSize,
			out IntPtr refOutAuthBuffer, out int refOutAuthBufferSize, ref bool fSave,
			CredentialsUiWindowsFlags uiWindowsFlags);
		internal static extern CredUiReturnCodes CredUIPromptForCredentials (ref CredentialUiInfo uiInfo, string targetName,
			IntPtr reserved1, int iError, StringBuilder userName, int maxUserName, StringBuilder password, int maxPassword,
			[MarshalAs (UnmanagedType.Bool)] ref bool pfSave, CredentialsUiFlags windowsFlags);
예제 #7
0
        public static bool DisplayModal(Program program,
                                        ref CredentialUiInfo credUiInfo,
                                        ref CredentialPackFlags authPackage,
                                        IntPtr packedAuthBufferPtr,
                                        uint packedAuthBufferSize,
                                        IntPtr inBufferPtr,
                                        int inBufferSize,
                                        bool saveCredentials,
                                        CredentialUiWindowsFlags flags,
                                        out string username,
                                        out string password)
        {
            if (program is null)
            {
                throw new ArgumentNullException(nameof(program));
            }

            int error;

            try
            {
                // open a standard Windows authentication dialog to acquire username + password credentials
                if ((error = CredUIPromptForWindowsCredentials(credInfo: ref credUiInfo,
                                                               authError: 0,
                                                               authPackage: ref authPackage,
                                                               inAuthBuffer: inBufferPtr,
                                                               inAuthBufferSize: (uint)inBufferSize,
                                                               outAuthBuffer: out packedAuthBufferPtr,
                                                               outAuthBufferSize: out packedAuthBufferSize,
                                                               saveCredentials: ref saveCredentials,
                                                               flags: flags)) != Win32Error.Success)
                {
                    program.Trace.WriteLine($"credential prompt failed ('{Win32Error.GetText(error)}').");

                    username = null;
                    password = null;

                    return(false);
                }

                // use `StringBuilder` references instead of string so that they can be written to
                var usernameBuffer = new StringBuilder(512);
                var domainBuffer   = new StringBuilder(256);
                var passwordBuffer = new StringBuilder(512);
                int usernameLen    = usernameBuffer.Capacity;
                int passwordLen    = passwordBuffer.Capacity;
                int domainLen      = domainBuffer.Capacity;

                // unpack the result into locally useful data
                if (!CredUnPackAuthenticationBuffer(flags: authPackage,
                                                    authBuffer: packedAuthBufferPtr,
                                                    authBufferSize: packedAuthBufferSize,
                                                    username: usernameBuffer,
                                                    maxUsernameLen: ref usernameLen,
                                                    domainName: domainBuffer,
                                                    maxDomainNameLen: ref domainLen,
                                                    password: passwordBuffer,
                                                    maxPasswordLen: ref passwordLen))
                {
                    username = null;
                    password = null;

                    error = Marshal.GetLastWin32Error();
                    program.Trace.WriteLine($"failed to unpack buffer ('{Win32Error.GetText(error)}').");

                    return(false);
                }

                program.Trace.WriteLine("successfully acquired credentials from user.");

                username = usernameBuffer.ToString();
                password = passwordBuffer.ToString();

                return(true);
            }
            finally
            {
                if (packedAuthBufferPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(packedAuthBufferPtr);
                }
            }
        }
예제 #8
0
        public static Credential PasswordPrompt(Program program, TargetUri targetUri, string message, string username)
        {
            if (program is null)
            {
                throw new ArgumentNullException(nameof(program));
            }
            if (targetUri is null)
            {
                throw new ArgumentNullException(nameof(targetUri));
            }
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (username is null)
            {
                throw new ArgumentNullException(nameof(username));
            }

            var credUiInfo = new CredentialUiInfo
            {
                BannerArt   = IntPtr.Zero,
                CaptionText = program.Title,
                MessageText = message,
                Parent      = program.ParentHwnd,
                Size        = Marshal.SizeOf(typeof(CredentialUiInfo))
            };
            var    flags                = CredentialUiWindowsFlags.Generic;
            var    authPackage          = CredentialPackFlags.None;
            var    packedAuthBufferPtr  = IntPtr.Zero;
            var    inBufferPtr          = IntPtr.Zero;
            uint   packedAuthBufferSize = 0;
            bool   saveCredentials      = false;
            int    inBufferSize         = 0;
            string password             = null;

            try
            {
                int error;

                // Execute with `null` to determine buffer size always returns false when determining
                // size, only fail if `inBufferSize` looks bad.
                CredPackAuthenticationBuffer(flags: authPackage,
                                             username: username,
                                             password: string.Empty,
                                             packedCredentials: IntPtr.Zero,
                                             packedCredentialsSize: ref inBufferSize);
                if (inBufferSize <= 0)
                {
                    error = Marshal.GetLastWin32Error();
                    program.Trace.WriteLine($"unable to determine credential buffer size ('{Win32Error.GetText(error)}').");

                    return(null);
                }

                inBufferPtr = Marshal.AllocHGlobal(inBufferSize);

                if (!CredPackAuthenticationBuffer(flags: authPackage,
                                                  username: username,
                                                  password: string.Empty,
                                                  packedCredentials: inBufferPtr,
                                                  packedCredentialsSize: ref inBufferSize))
                {
                    error = Marshal.GetLastWin32Error();
                    program.Trace.WriteLine($"unable to write to credential buffer ('{Win32Error.GetText(error)}').");

                    return(null);
                }

                if (program.ModalPromptDisplayDialog(ref credUiInfo,
                                                     ref authPackage,
                                                     packedAuthBufferPtr,
                                                     packedAuthBufferSize,
                                                     inBufferPtr,
                                                     inBufferSize,
                                                     saveCredentials,
                                                     flags,
                                                     out username,
                                                     out password))
                {
                    return(new Credential(username, password));
                }
            }
            finally
            {
                if (inBufferPtr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(inBufferPtr);
                }
            }

            return(null);
        }
예제 #9
0
 public static extern CredentialUiResult CredUIPromptForCredentials(ref CredentialUiInfo credUiInfo, string targetName, IntPtr reserved, uint authError, StringBuilder username, int usernameMaxLen, StringBuilder password, int passwordMaxLen, ref bool saveCredentials, CredentialUiFlags flags);
 internal static extern WindowsCredentialPromptReturnCode CredUIPromptForWindowsCredentials(ref CredentialUiInfo uiInfo,
                                                                                            int authError, ref int authPackage, IntPtr inAuthBuffer, uint inAuthBufferSize,
                                                                                            out IntPtr refOutAuthBuffer, out int refOutAuthBufferSize, ref bool fSave,
                                                                                            CredentialsUiWindowsFlags uiWindowsFlags);
 internal static extern CredUiReturnCodes CredUIPromptForCredentials(ref CredentialUiInfo uiInfo, string targetName,
                                                                     IntPtr reserved1, int iError, StringBuilder userName, int maxUserName, StringBuilder password, int maxPassword,
                                                                     [MarshalAs(UnmanagedType.Bool)] ref bool pfSave, CredentialsUiFlags windowsFlags);