/// <summary>
 /// Provides the flags that specify how the Windows credential manager handles the credentials.
 /// </summary>
 private CREDMGMTUI.FLAGS GetFlags()
 {
     CREDMGMTUI.FLAGS flags = CREDMGMTUI.FLAGS.GENERIC_CREDENTIALS |
                              CREDMGMTUI.FLAGS.EXPECT_CONFIRMATION |
                              CREDMGMTUI.FLAGS.EXCLUDE_CERTIFICATES;
     return(flags);
 }
        /// <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));
            }
        }