Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf password box into a SecureString in C#

Tags:

c#

wpf

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?

like image 621
Anthony Avatar asked Jun 04 '10 23:06

Anthony


2 Answers

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.

like image 191
hemp Avatar answered Sep 28 '22 12:09

hemp


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
like image 40
Pharabus Avatar answered Sep 28 '22 10:09

Pharabus