0

I'm trying to make it so that when I check my CheckBox, The current password typed into the passwordbox will be displayed in plaintext rather than the current asterisk.

Current code:

        private void chkBoxShowPassword_Checked(object sender, RoutedEventArgs e)
        {
            if (chkBoxShowPassword.IsChecked == true)
            {
                txtPassword.Password.ToString();
                // txtPassword.PasswordChar = 'h';
            }

            else
            {
               
            }
        }

I tried the "ToString" method but that didnt do anything when i checked the box & I also tried setting the passwordChar to "h" but obviously that doesnt store the password in plaintext but just sets every character to be the letter "h".

  • You'll probably need to have both a `TextBox` and a `PasswordBox`, and hide one / show the other depending on the `CheckBox`'s value – canton7 Mar 29 '22 at 12:42
  • If you want to _show_ the text as plain text (which, is a really bad idea), just use a standard text box. – gunr2171 Mar 29 '22 at 12:43
  • @gunr2171: The scenario the OP is describing is an input box that can switch between masked and visible passwords. – O. R. Mapper Mar 29 '22 at 12:44
  • @gunr2171 That post is all related to Win Forms. I am using WPF. "txtPassword.PasswordChar = '\0';" doesn't work with WPF. – LearningCoder44 Mar 29 '22 at 12:55
  • How about the link that starts with "C# / WPF Unmask..."? – gunr2171 Mar 29 '22 at 12:56
  • PasswordBox.Password is a property. It only returns the currently entered value (or sets it). You must wrap a PasswordBox into a Grid and overlay it with a TextBlock or TextBox (using the ZIndex). Toggle its visibility and set the Text with the value returned from the PasswordBox.Pasword. Note that accessing the Password property will load the plain text password into the memory (which can be read by any application)! You probably don't want this. – BionicCode Mar 29 '22 at 13:33
  • If your application runs on a Windows machine, use the Windows authentication instead. It is by far more secure. Otherwise use some OAuth service. If you make your application install on user level, you don't need authentication at all (as Windows enforces account authentication). What you are currently doing is silently exposing the user's password i.e. privacy data, to the public... – BionicCode Mar 29 '22 at 13:33

0 Answers0