简体   繁体   中英

wpf password box into a SecureString in C#

I am attempting to get the data from a wpf password box into a secure string. How is that done? what i have so far:

 SecureString pass = new SecureString();
        pass.AppendChar(pbox1.Password);

this of course does not work, so how would I get the password data without creating a regular string?

Per MSDN :

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.

You should avoid using the Password property unless you absolutely need a plaintext version of the string. In this case, retrieve the SecureString directly.

you need to read each character in

SecureString pass = new SecureString();

foreach (char c in pbox1.Password)
{
  pass.AppendChar(c);
}

or more securely use the SecurePassword property

SecureString pass = pbox1.SecurePassword
SecureString pass = pbox1.SecurePassword.Copy();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM