Inheritance: LocalWindowsHook
Esempio n. 1
0
        public DialogResult ShowDialog( IWin32Window owner, bool retry )
        {
            if ( !CanCallCredUI )
                return ShowPseudoDialog ( owner, retry );
            else {
                LocalCbtHook hook = new LocalCbtHook ( );
                try {
                    hook.WindowActivated += new LocalCbtHook.CbtEventHandler ( hook_WindowActivated );
                    hook.WindowCreated += new LocalCbtHook.CbtEventHandler ( hook_WindowCreated );
                    hook.WindowDestroyed += new LocalCbtHook.CbtEventHandler ( hook_WindowDestroyed );
                    hook.Install ( );

                    CredUIInfo uiInfo = new CredUIInfo ( owner, text, caption );
                    StringBuilder sbUsername = new StringBuilder ( username, MaxUsername );
                    StringBuilder sbPassword = new StringBuilder ( password, MaxPassword );
                    int nSave = Convert.ToInt32 ( save );
                    CredUIFlags flags = CredUIFlags.GenericCredentials;
                    if ( expectConfirmation )
                        flags |= CredUIFlags.ExpectConfirmation;
                    if ( forceUI )
                        flags |= CredUIFlags.AlwaysShowUI;
                    if ( retry )
                        flags |= CredUIFlags.IncorrectPassword;
                    programmaticCloseResult = DialogResult.None;
                    CredUIReturnCodes retVal = CredUIPromptForCredentials ( ref uiInfo, targetName, IntPtr.Zero, 0, sbUsername, MaxUsername, sbPassword, MaxPassword, ref nSave, flags );
                    save = Convert.ToBoolean ( nSave );

                    if ( programmaticCloseResult != DialogResult.None )
                        return programmaticCloseResult;
                    switch ( retVal ) {
                        case CredUIReturnCodes.NoError:
                            username = sbUsername.ToString ( );
                            password = sbPassword.ToString ( );
                            save = Convert.ToBoolean ( nSave );
                            return DialogResult.OK;
                        case CredUIReturnCodes.Cancelled:
                            return DialogResult.Cancel;
                        default:
                            return DialogResult.Abort;
                    }
                } finally {
                    hook.WindowActivated -= new LocalCbtHook.CbtEventHandler ( hook_WindowActivated );
                    hook.WindowCreated -= new LocalCbtHook.CbtEventHandler ( hook_WindowCreated );
                    hook.WindowDestroyed -= new LocalCbtHook.CbtEventHandler ( hook_WindowDestroyed );
                    hook.Uninstall ( );
                }
            }
        }
Esempio n. 2
0
        public DialogResult ShowDialog(IWin32Window owner, bool retry)
        {
            if (!CanCallCredUI)
            {
                return(ShowPseudoDialog(owner, retry));
            }
            else
            {
                LocalCbtHook hook = new LocalCbtHook( );
                try {
                    hook.WindowActivated += new LocalCbtHook.CbtEventHandler(hook_WindowActivated);
                    hook.WindowCreated   += new LocalCbtHook.CbtEventHandler(hook_WindowCreated);
                    hook.WindowDestroyed += new LocalCbtHook.CbtEventHandler(hook_WindowDestroyed);
                    hook.Install( );

                    CredUIInfo    uiInfo     = new CredUIInfo(owner, text, caption);
                    StringBuilder sbUsername = new StringBuilder(username, MaxUsername);
                    StringBuilder sbPassword = new StringBuilder(password, MaxPassword);
                    int           nSave      = Convert.ToInt32(save);
                    CredUIFlags   flags      = CredUIFlags.GenericCredentials;
                    if (expectConfirmation)
                    {
                        flags |= CredUIFlags.ExpectConfirmation;
                    }
                    if (forceUI)
                    {
                        flags |= CredUIFlags.AlwaysShowUI;
                    }
                    if (retry)
                    {
                        flags |= CredUIFlags.IncorrectPassword;
                    }
                    programmaticCloseResult = DialogResult.None;
                    CredUIReturnCodes retVal = CredUIPromptForCredentials(ref uiInfo, targetName, IntPtr.Zero, 0, sbUsername, MaxUsername, sbPassword, MaxPassword, ref nSave, flags);
                    save = Convert.ToBoolean(nSave);

                    if (programmaticCloseResult != DialogResult.None)
                    {
                        return(programmaticCloseResult);
                    }
                    switch (retVal)
                    {
                    case CredUIReturnCodes.NoError:
                        username = sbUsername.ToString( );
                        password = sbPassword.ToString( );
                        save     = Convert.ToBoolean(nSave);
                        return(DialogResult.OK);

                    case CredUIReturnCodes.Cancelled:
                        return(DialogResult.Cancel);

                    default:
                        return(DialogResult.Abort);
                    }
                } finally {
                    hook.WindowActivated -= new LocalCbtHook.CbtEventHandler(hook_WindowActivated);
                    hook.WindowCreated   -= new LocalCbtHook.CbtEventHandler(hook_WindowCreated);
                    hook.WindowDestroyed -= new LocalCbtHook.CbtEventHandler(hook_WindowDestroyed);
                    hook.Uninstall( );
                }
            }
        }