C# how to get text value from PasswordBox?

Viewed 86985

I have a PasswordBox. how can I get the input value from the PasswordBox after the input has been finished?

6 Answers

You can get it from the Password property.

If using a MaskedTextbox you can use the .text property. For example:

private void btnOk_Click(object sender, EventArgs e)
{
    if ( myMaskedTextbox.Text.Equals(PASSWORD) )
    {
        //do something
    }         

}

You have to give a name to your PasswordBox.

<PasswordBox Name="pwdBox"/>

Then you can access the password as plain text in the .xaml.cs file by using

var plainPassword = pwdBox.password;

I suggest you read this answer to a similar question where you get to know why you mustn't store this property value in any variable.

However, I found in documentation information about SecureString.

When you get the Password property value, you expose the password as plain text in memory. To avoid this potential security risk, use the SecurePassword property to get the password as a SecureString.

source of this quotation

Correct me if I am wrong.

Greetings. Jan.

Related