// Token: 0x06000AD2 RID: 2770 RVA: 0x0002214C File Offset: 0x0002034C
        public override DialogResult ShowDialog(IntPtr owner)
        {
            base.CheckNotDisposed();
            if (string.IsNullOrEmpty(base.Title) && string.IsNullOrEmpty(base.Message))
            {
                throw new InvalidOperationException("Title or Message should always be set.");
            }
            if (!this.IsWinVistaOrHigher)
            {
                throw new InvalidOperationException("This Operating System does not support this prompt.");
            }
            uint   num         = 0u;
            IntPtr intPtr      = IntPtr.Zero;
            int    num2        = 0;
            bool   saveChecked = base.SaveChecked;

            NativeMethods.CREDUI_INFO credui_INFO = this.CreateCREDUI_INFO(owner);
            if (!string.IsNullOrEmpty(base.Username) || !string.IsNullOrEmpty(SecureStringHelper.CreateString(base.SecurePassword)))
            {
                NativeMethods.CredPackAuthenticationBuffer(0, new StringBuilder(base.Username), new StringBuilder(SecureStringHelper.CreateString(base.SecurePassword)), intPtr, ref num2);
                if (Marshal.GetLastWin32Error() == 122)
                {
                    intPtr = Marshal.AllocCoTaskMem(num2);
                    if (!NativeMethods.CredPackAuthenticationBuffer(0, new StringBuilder(base.Username), new StringBuilder(SecureStringHelper.CreateString(base.SecurePassword)), intPtr, ref num2))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error(), "There was an issue with the given Username or Password.");
                    }
                }
            }
            IntPtr intPtr2;
            uint   cbAuthBuffer;

            NativeMethods.CredUIReturnCodes credUIReturnCodes;
            try
            {
                credUIReturnCodes = NativeMethods.CredUIPromptForWindowsCredentials(ref credui_INFO, base.ErrorCode, ref num, intPtr, (uint)num2, out intPtr2, out cbAuthBuffer, ref saveChecked, base.DialogFlags);
                base.SaveChecked  = saveChecked;
            }
            catch (EntryPointNotFoundException innerException)
            {
                throw new InvalidOperationException("This functionality is not supported by this operating system.", innerException);
            }
            if (credUIReturnCodes <= NativeMethods.CredUIReturnCodes.ERROR_INVALID_FLAGS)
            {
                if (credUIReturnCodes <= NativeMethods.CredUIReturnCodes.ERROR_INSUFFICIENT_BUFFER)
                {
                    if (credUIReturnCodes != NativeMethods.CredUIReturnCodes.ERROR_INVALID_PARAMETER && credUIReturnCodes != NativeMethods.CredUIReturnCodes.ERROR_INSUFFICIENT_BUFFER)
                    {
                        goto IL_198;
                    }
                }
                else if (credUIReturnCodes != NativeMethods.CredUIReturnCodes.ERROR_BAD_ARGUMENTS && credUIReturnCodes != NativeMethods.CredUIReturnCodes.ERROR_INVALID_FLAGS)
                {
                    goto IL_198;
                }
            }
            else if (credUIReturnCodes <= NativeMethods.CredUIReturnCodes.ERROR_CANCELLED)
            {
                if (credUIReturnCodes != NativeMethods.CredUIReturnCodes.ERROR_NOT_FOUND)
                {
                    if (credUIReturnCodes != NativeMethods.CredUIReturnCodes.ERROR_CANCELLED)
                    {
                        goto IL_198;
                    }
                    return(DialogResult.Cancel);
                }
            }
            else if (credUIReturnCodes != NativeMethods.CredUIReturnCodes.ERROR_NO_SUCH_LOGON_SESSION && credUIReturnCodes != NativeMethods.CredUIReturnCodes.ERROR_INVALID_ACCOUNT_NAME)
            {
                goto IL_198;
            }
            throw new InvalidOperationException("Invalid properties were specified.", new Win32Exception(Marshal.GetLastWin32Error()));
IL_198:
            int num3 = 1000;
            int           num4           = 1000;
            int           num5           = 1000;
            StringBuilder stringBuilder  = new StringBuilder(1000);
            StringBuilder stringBuilder2 = new StringBuilder(1000);
            StringBuilder pszDomainName  = new StringBuilder(1000);

            if (NativeMethods.CredUnPackAuthenticationBuffer(0, intPtr2, cbAuthBuffer, stringBuilder, ref num3, pszDomainName, ref num5, stringBuilder2, ref num4))
            {
                NativeMethods.CoTaskMemFree(intPtr2);
                base.Username = stringBuilder.ToString();
                base.Password = stringBuilder2.ToString();
                if (stringBuilder2.Length > 0)
                {
                    stringBuilder2.Remove(0, stringBuilder2.Length);
                }
            }
            return(DialogResult.OK);
        }
Exemple #2
0
        public override DialogResult ShowDialog(IntPtr owner)
        {
            CheckNotDisposed();
            if (string.IsNullOrEmpty(base.Title) && string.IsNullOrEmpty(base.Message))
            {
                throw new InvalidOperationException("Title or Message should always be set.");
            }
            if (!IsWinVistaOrHigher)
            {
                throw new InvalidOperationException("This Operating System does not support this prompt.");
            }
            uint   authPackage          = 0u;
            IntPtr intPtr               = IntPtr.Zero;
            int    pcbPackedCredentials = 0;
            bool   fSave = base.SaveChecked;

            NativeMethods.CREDUI_INFO notUsedHere = CreateCREDUI_INFO(owner);
            if (!string.IsNullOrEmpty(base.Username) || !string.IsNullOrEmpty(SecureStringHelper.CreateString(base.SecurePassword)))
            {
                NativeMethods.CredPackAuthenticationBuffer(0, new StringBuilder(base.Username), new StringBuilder(SecureStringHelper.CreateString(base.SecurePassword)), intPtr, ref pcbPackedCredentials);
                if (Marshal.GetLastWin32Error() == 122)
                {
                    intPtr = Marshal.AllocCoTaskMem(pcbPackedCredentials);
                    if (!NativeMethods.CredPackAuthenticationBuffer(0, new StringBuilder(base.Username), new StringBuilder(SecureStringHelper.CreateString(base.SecurePassword)), intPtr, ref pcbPackedCredentials))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error(), "There was an issue with the given Username or Password.");
                    }
                }
            }
            NativeMethods.CredUIReturnCodes credUIReturnCodes;
            IntPtr refOutAuthBuffer;
            uint   refOutAuthBufferSize;

            try
            {
                credUIReturnCodes = NativeMethods.CredUIPromptForWindowsCredentials(ref notUsedHere, base.ErrorCode, ref authPackage, intPtr, (uint)pcbPackedCredentials, out refOutAuthBuffer, out refOutAuthBufferSize, ref fSave, base.DialogFlags);
                base.SaveChecked  = fSave;
            }
            catch (EntryPointNotFoundException innerException)
            {
                throw new InvalidOperationException("This functionality is not supported by this operating system.", innerException);
            }
            switch (credUIReturnCodes)
            {
            case NativeMethods.CredUIReturnCodes.ERROR_CANCELLED:
                return(DialogResult.Cancel);

            case NativeMethods.CredUIReturnCodes.ERROR_INVALID_PARAMETER:
            case NativeMethods.CredUIReturnCodes.ERROR_INSUFFICIENT_BUFFER:
            case NativeMethods.CredUIReturnCodes.ERROR_BAD_ARGUMENTS:
            case NativeMethods.CredUIReturnCodes.ERROR_INVALID_FLAGS:
            case NativeMethods.CredUIReturnCodes.ERROR_NOT_FOUND:
            case NativeMethods.CredUIReturnCodes.ERROR_NO_SUCH_LOGON_SESSION:
            case NativeMethods.CredUIReturnCodes.ERROR_INVALID_ACCOUNT_NAME:
                throw new InvalidOperationException("Invalid properties were specified.", new Win32Exception(Marshal.GetLastWin32Error()));

            default:
            {
                int           pcchMaxUserName  = 1000;
                int           pcchMaxPassword  = 1000;
                int           pcchMaxDomainame = 1000;
                StringBuilder stringBuilder    = new StringBuilder(1000);
                StringBuilder stringBuilder2   = new StringBuilder(1000);
                StringBuilder pszDomainName    = new StringBuilder(1000);
                if (NativeMethods.CredUnPackAuthenticationBuffer(0, refOutAuthBuffer, refOutAuthBufferSize, stringBuilder, ref pcchMaxUserName, pszDomainName, ref pcchMaxDomainame, stringBuilder2, ref pcchMaxPassword))
                {
                    NativeMethods.CoTaskMemFree(refOutAuthBuffer);
                    base.Username = stringBuilder.ToString();
                    base.Password = stringBuilder2.ToString();
                    if (stringBuilder2.Length > 0)
                    {
                        stringBuilder2.Remove(0, stringBuilder2.Length);
                    }
                }
                return(DialogResult.OK);
            }
            }
        }