简体   繁体   English

设置密码框的文本

[英]Setting the text of a PasswordBox

I am creating an application that requires user login (username + password).我正在创建一个需要用户登录(用户名 + 密码)的应用程序。 As this is a secure application, I'm using WPF's PasswordBox.SecurePassword to securely create a hash of the password (using SHA512CryptoServiceProvider, hashing from a custom Stream) to use as a password for Rfc2898DeriveBytes.由于这是一个安全的应用程序,我使用 WPF 的 PasswordBox.SecurePassword 来安全地创建密码的散列(使用 SHA512CryptoServiceProvider,从自定义流中散列)以用作 Rfc2898DeriveBytes 的密码。 Let's say I have an AES-256-CBC implementation which securely retrieves and stores key+IV so that is not an issue.假设我有一个 AES-256-CBC 实现,它可以安全地检索和存储 key+IV,因此这不是问题。
I am currently implementing a way for users to be able to store their password so they don't have to put them every time they log in. So far I am able to store the password securely using the AES custom implementation and I can read the password securely (that is, without ever putting it completely in Memory in plain text).我目前正在实施一种方法让用户能够存储他们的密码,这样他们就不必在每次登录时都输入密码。到目前为止,我能够使用 AES 自定义实现安全地存储密码,并且我可以阅读密码安全(即,无需将其以纯文本形式完全放入内存中)。
What I am having problem is when I try to replace the text in the PasswordBox.我遇到的问题是当我尝试替换 PasswordBox 中的文本时。 PasswordBox.SecurePassword is read-only and I have tried to use PasswordBox.SecurePassword.AppendChar(someChar) but the field/value remains empty. PasswordBox.SecurePassword 是只读的,我尝试使用 PasswordBox.SecurePassword.AppendChar(someChar) 但该字段/值仍然为空。 I imagine that PasswordBox.SecurePassword only returns a copy (SecureString.Copy()) and not the original variable ( looking at .NET source code confirms this ).我想 PasswordBox.SecurePassword 只返回一个副本 (SecureString.Copy()) 而不是原始变量( 查看 .NET 源代码证实了这一点)。
Is there any way I can achieve what I want or do I need to create a custom WPF control?有什么方法可以实现我想要的或者我需要创建自定义 WPF 控件吗?

To set the text of the PasswordBox in WPF use the following code:要在 WPF 中设置 PasswordBox 的文本,请使用以下代码:

PasswordBox1.Password = "yourpassword";

You don't need to use the SecurePassword function as long as you are able to get the password in plain text then you will be able to set it using the Password function.您不需要使用 SecurePassword 功能,只要您能够以纯文本形式获取密码,然后您就可以使用 Password 功能进行设置。 As you mentioned the SecurePassword function is read only this is because this retrieves the password in a secure form, it does not allow you to set the text in the PasswordBox.正如您提到的 SecurePassword 函数是只读的,这是因为它以安全的形式检索密码,它不允许您在 PasswordBox 中设置文本。 MSDN Documentation MSDN 文档

If you want to be able to put the secure password in the PasswordBox you can also use this method, but you cannot put it in plain text, because you'd effectively be asking to break the encryption, but if you have the key and you can decrypt it then do that first and then set the text in the PasswordBox.如果您希望能够将安全密码放在 PasswordBox 中,您也可以使用此方法,但不能以纯文本形式输入,因为您实际上是在要求破解加密,但是如果您有密钥并且可以解密,然后先解密,然后在 PasswordBox 中设置文本。

To view the password entered in the PasswordBox in plain text use the following code:要以纯文本形式查看在 PasswordBox 中输入的密码,请使用以下代码:

PasswordBox1.Password;

This can be considered to be solved.这个可以考虑解决。
We decided to use an animation to show the user that they don't need to put the password again.我们决定使用动画向用户展示他们不需要再次输入密码。
It's a shame you cannot programmatically add characters to the SecurePassword property like you can do with a normal SecureString.很遗憾,您无法像使用普通 SecureString 那样以编程方式向 SecurePassword 属性添加字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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