wpf password box into a SecureString in C#

前端 未结 3 1974
一整个雨季
一整个雨季 2021-01-05 20:38

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();         


        
相关标签:
3条回答
  • 2021-01-05 21:20

    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.

    0 讨论(0)
  • 2021-01-05 21:24

    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
    
    0 讨论(0)
  • 2021-01-05 21:27
    SecureString pass = pbox1.SecurePassword.Copy();
    
    0 讨论(0)
提交回复
热议问题