Example #1
0
        /// <summary>
        /// Calls the CredUIConfirmCredentials function which confirms that the credentials
        /// provided for the target should be kept in the case they are persisted.</summary>
        /// <param name="value">A value of true if the credentials should be persisted.</param>
        public void Confirm(bool value)
        {
            switch (CREDMGMTUI.ConfirmCredentials(this.Target, value))
            {
            case CREDMGMTUI.ReturnCodes.NO_ERROR:
                break;

            case CREDMGMTUI.ReturnCodes.ERROR_INVALID_PARAMETER:
                break;

            default:
                throw new ApplicationException("Credential confirmation failed.");
            }
        }
Example #2
0
        /// <summary>
        /// Calls the CredUICmdLinePromptForCredentials function that will either prompt for
        /// credentials or get the stored credentials for the specified target.
        /// </summary>
        /// <returns>The DialogResult that indicates the results of using CredUI.</returns>
        /// <remarks>
        /// Sets the user name, password and persistence state of the dialog.
        /// </remarks>
        internal DialogResult ShowPrompt()
        {
            StringBuilder name        = new StringBuilder(CREDMGMTUI.MAX_USERNAME_LENGTH);
            StringBuilder pwd         = new StringBuilder(CREDMGMTUI.MAX_PASSWORD_LENGTH);
            int           saveChecked = Convert.ToInt32(this.SaveChecked);

            CREDMGMTUI.FLAGS flags = GetFlags();

            Console.WriteLine("Enter your Exchange Online user principal name for the Exchange Web Services stored credential ([email protected])...");

            lock (this)
            {
                // Call CredUi.dll to prompt for credentials. If the credentials for the target have already
                // been saved in the credential manager, then this method will return the credentials
                // and the UI will not be displayed.
                CREDMGMTUI.ReturnCodes code = CREDMGMTUI.CredUICmdLinePromptForCredentials(
                    this.Target,
                    IntPtr.Zero, 0,
                    name, CREDMGMTUI.MAX_USERNAME_LENGTH,
                    pwd, CREDMGMTUI.MAX_PASSWORD_LENGTH,
                    ref saveChecked,
                    flags
                    );

                // Convert the password returned by the credential manager to a secure string.
                this.Password = ConvertToSecureString(pwd);

                // Get the user name stored in the credential manager.
                this.Name = name.ToString();

                // Get the value that indicates whether the credentials are persisted
                // in the credential manager.
                this.SaveChecked = Convert.ToBoolean(saveChecked);

                return(GetCredPromptResult(code));
            }
        }
Example #3
0
        /// <summary>
        /// Returns a DialogResult based on the code returned by the credential manager.
        /// </summary>
        /// <param name="code">The credential return code provided by the credential manager.</param>
        private DialogResult GetCredPromptResult(CREDMGMTUI.ReturnCodes code)
        {
            DialogResult result;
            switch (code)
            {
                case CREDMGMTUI.ReturnCodes.NO_ERROR:
                    result = DialogResult.OK;
                    break;

                case CREDMGMTUI.ReturnCodes.ERROR_CANCELLED:
                    result = DialogResult.Cancel;
                    break;

                case CREDMGMTUI.ReturnCodes.ERROR_NO_SUCH_LOGON_SESSION:
                    throw new ApplicationException("The credential manager cannot be used.");

                case CREDMGMTUI.ReturnCodes.ERROR_INVALID_PARAMETER:
                    throw new ApplicationException(@"Invalid parameter. See http://msdn.microsoft.com/en-us/library/windows/desktop/aa375177(v=vs.85).aspx");

                case CREDMGMTUI.ReturnCodes.ERROR_INVALID_FLAGS:
                    throw new ApplicationException("Invalid flags.");

                default:
                    throw new ApplicationException("The credential manager returned an unknown return code.");
            }
            return result;
        }