deception1 deception1 - 2 years ago 86
C# Question

Use value of passwordbox as to a variable

i have a password box and i want to get the input data to check for verification.

My passwordbox c# code

public void textBox2_TextInput(object sender, TextCompositionEventArgs e)
{
//pass = textBox2.ToString();
}


and the xaml code

<PasswordBox Name="textBox2"
PasswordChar="*"
TextInput="textBox2_TextInput" />


this is what i have written to capture the password

private void loginbutton_Click(object sender, RoutedEventArgs e)
{
usr = textBox1.Text;
SecureString passdat =textBox2.SecurePassword;
pass = passdat.ToString();
}


it returns null.This is a dummy demo so no encryption is required.I was using a text box earlier and the verification worked.using a password box just complicated things.

Answer Source

The SecureString class doesn't allow you to see the value; that's the whole point of it. If you want to be able to work with the value entered into the PasswordBox, use the Password member of PasswordBox instead of the SecurePassword member:

private void loginbutton_Click(object sender, RoutedEventArgs e)
{
    usr = textBox1.Text;
    String pass = textBox2.Password;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download