C# how to get text value from PasswordBox?
I have a PasswordBox
. how can I get the input value from the PasswordBox
after the input has been finishe开发者_开发百科d?
You can get it from the Password
property.
You may extract it from Password
property:
passwordBox.Password.ToString()
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
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
}
}
I use below code to get the length of PasswordBox
PasswordVariableName.Password.Length
It will certainly work on wp8
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.
精彩评论