问题
I have an existing SecureString that I would like to put into a PasswordBox without revealing the .Password. Can this be done? For example:
tbPassword.SecurePassword = DecryptString(Properties.Settings.Default.proxyPassword);
In this case DecryptString produces a SecureString. However, SecurePassword is a read-only property so I can't assign a value to it.
回答1:
You can't.
However, what you can do is put placeholder text in it's place (it can even be "placeholder"
, we are only using it to make a few dots to show up in the box).
After you put the placeholder in, when you go to retrieve the "current password" somewhere in your program first check if the PasswordChanged event has fired since you entered the placeholder password. If the event has not fired use the old stored password, if the event has fired use the current password from the SecurePassword property of PasswordBox
.
来源:https://stackoverflow.com/questions/19747195/put-securestring-into-passwordbox