PasswordInputPrompt throws ArgumentOutOfRangeException when pasting chars to password field

Apr 23, 2013 at 11:40 AM
The PasswordInputPrompt only works when typing in the password chars one by one. If you try to paste a password stored in clipboard you get ArgumentOutOfRangeException .

A fix would be to change the following lines:

Line 56:
var selectionIndex = selectionStart - 1;
To
var selectionIndex = selectionStart - diff;
Line 64:
replacementString.Insert(0, PasswordChar.ToString(CultureInfo.InvariantCulture), InputBox.Text.Length - 1);
To
replacementString.Insert(0, PasswordChar.ToString(CultureInfo.InvariantCulture), InputBox.Text.Length - diff);
Or even better, to make the control act as the default PasswordBox is to have the pasted chars immediately show as * without the delayed overwrite. Something like this:
......
      } else if (diff > 0) {
          if (diff > 1) { // Paste operation
              var tempSelectionStart = InputBox.SelectionStart;
              var pasteIndex = InputBox.SelectionStart - diff;
              var pastedChars = InputBox.Text.Substring(pasteIndex, diff);
              _inputText.Insert(pasteIndex, pastedChars);
              var replacementString = new StringBuilder();
              replacementString.Insert(0, PasswordChar.ToString(CultureInfo.InvariantCulture), InputBox.Text.Length);
              InputBox.Text = replacementString.ToString();
              InputBox.SelectionStart = tempSelectionStart;
              Value = _inputText.ToString();
              return;
          }

          // get new chars
          // append onto SB
          // set value
          // update InputBox with *
......
Coordinator
Apr 23, 2013 at 4:39 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Apr 27, 2013 at 6:52 PM
Fixed in 79138, refactored suggested fix to reduce repeating of code. good catch and thanks for helping with the fix.