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);
 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);
示例#3
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);
                }
            }
        }