在.NET中,我们有SecureString类,在你尝试使用它之前一切都很好,因为(例如)散列字符串,你需要明文.在编写一个将散列SecureString的函数时,我已经开始了,给定一个带有字节数组并输出字节数组的散列函数.
private static byte[] HashSecureString(SecureString ss, Func<byte[], byte[]> hash)
{
// Convert the SecureString to a BSTR
IntPtr bstr = Marshal.SecureStringToBSTR(ss);
// BSTR contains the length of the string in bytes in an
// Int32 stored in the 4 bytes prior to the BSTR pointer
int length = Marshal.ReadInt32(bstr, -4);
// Allocate a byte array to copy the string into
byte[] bytes = new byte[length];
// Copy the BSTR to the byte array
Marshal.Copy(bstr, bytes, 0, length);
// Immediately destroy …Run Code Online (Sandbox Code Playgroud) 我已经读过,出于安全原因, WPF PasswordBox中的密码没有用于绑定密码的依赖项属性.尽管如此,无论如何都有办法绑定它.
MVVM模式的用户需要这种数据绑定; viewmodel无法在不破坏模式的情况下直接触摸PasswordBox.在MVVM设置中使用PasswordBoxes的一种方法是将整个PasswordBox控件传递给ViewModel,但这无论如何都会破坏模式.绑定密码可能是使用MVVM处理密码的最简洁方法.
有一个反对绑定密码的论据,因为这会将明文密码保存在未加密的内存中,直到它被垃圾收集.然而,我看到它的方式是,从您访问该Password属性的那一刻起,密码就会存储在未加密的内存中.这个观点(或类似的)似乎在这个问题中被借调.当然,它会在更短的时间内在没有绑定的情况下存在(不是登录形式有长期存在的倾向),但风险仍然存在.
鉴于这些论点,绑定密码真的是一个坏主意吗?为什么?