65

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

Tasnim Fabiha
  • 1,148
  • 1
  • 17
  • 31
5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210

6 Answers6

124

You can get it from the Password property.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
17

You may extract it from Password property:

passwordBox.Password.ToString()
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Death Zone
  • 171
  • 1
  • 2
3

You may not want to store the password in clear text in memory, from the msdn doc you should use SecurePassword in order to prevent that.

Example: SecureString myPass = passwordBox.SecurePassword

https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.passwordbox.securepassword

Kols
  • 3,641
  • 2
  • 34
  • 42
jiciftw
  • 65
  • 1
  • 6
2

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
    }         

}
Roast
  • 1,715
  • 5
  • 24
  • 32
1

I use below code to get the length of PasswordBox

PasswordVariableName.Password.Length

It will certainly work on wp8

Ehtesham
  • 25
  • 5
0

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.

Jan Urbaś
  • 19
  • 2