/// <summary>Returns a DialogResult indicating the user action.</summary> /// <param name="owner">The System.Windows.Forms.IWin32Window the dialog will display in front of.</param> /// <remarks> /// Sets the name, password and SaveChecked accessors to the state of the dialog as it was dismissed by the user. /// </remarks> private DialogResult ShowDialog(IWin32Window owner) { // set the api call parameters StringBuilder name = new StringBuilder(CredUi.MAX_USERNAME_LENGTH); name.Append(Name); StringBuilder password = new StringBuilder(CredUi.MAX_PASSWORD_LENGTH); password.Append(Password); int saveChecked = Convert.ToInt32(SaveChecked); CredUi.INFO info = GetInfo(owner); CredUi.CredFlags credFlags = GetFlags(); // make the api call CredUi.ReturnCodes code = CredUi.CredUIPromptForCredentials( ref info, Target, IntPtr.Zero, 0, name, CredUi.MAX_USERNAME_LENGTH, password, CredUi.MAX_PASSWORD_LENGTH, ref saveChecked, credFlags ); // clean up resources if (Banner != null) { DeleteObject(info.hbmBanner); } // set the accessors from the api call parameters Name = name.ToString(); Password = password.ToString(); SaveChecked = Convert.ToBoolean(saveChecked); return(GetDialogResult(code)); }
/// <summary>Returns a DialogResult from the specified code.</summary> /// <param name="code">The credential return code.</param> private DialogResult GetDialogResult(CredUi.ReturnCodes code) { DialogResult result; switch (code) { case CredUi.ReturnCodes.NO_ERROR: result = DialogResult.OK; break; case CredUi.ReturnCodes.ERROR_CANCELLED: result = DialogResult.Cancel; break; case CredUi.ReturnCodes.ERROR_NO_SUCH_LOGON_SESSION: throw new ApplicationException("No such logon session."); case CredUi.ReturnCodes.ERROR_NOT_FOUND: throw new ApplicationException("Not found."); case CredUi.ReturnCodes.ERROR_INVALID_ACCOUNT_NAME: throw new ApplicationException("Invalid account name."); case CredUi.ReturnCodes.ERROR_INSUFFICIENT_BUFFER: throw new ApplicationException("Insufficient buffer."); case CredUi.ReturnCodes.ERROR_INVALID_PARAMETER: throw new ApplicationException("Invalid parameter."); case CredUi.ReturnCodes.ERROR_INVALID_FLAGS: throw new ApplicationException("Invalid flags."); default: throw new ApplicationException("Unknown credential result encountered."); } return(result); }