C#如何从PasswordBox获取文本值?

发布于 2024-08-22 23:59:10 字数 75 浏览 5 评论 0 原文

我有一个PasswordBox。输入完成后如何从PasswordBox中获取输入值?

I have a PasswordBox. how can I get the input value from the PasswordBox after the input has been finished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你与昨日 2024-08-29 23:59:11

您可以从 Password 属性获取它。

You can get it from the Password property.

小ぇ时光︴ 2024-08-29 23:59:11

您可以从 Password 属性中提取它:

passwordBox.Password.ToString()

You may extract it from Password property:

passwordBox.Password.ToString()
抽个烟儿 2024-08-29 23:59:11

您可能不希望将密码以明文形式存储在内存中,根据 msdn 文档,您应该使用 SecurePassword 来防止这种情况发生。

示例: SecureString myPass = passwordBox.SecurePassword

https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.passwordbox.securepassword

You may not want to store the password in clear text in memory, from the msdn doc you should use SecurePassword in order to prevent that.

Example: SecureString myPass = passwordBox.SecurePassword

https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.passwordbox.securepassword

北方的韩爷 2024-08-29 23:59:11

如果使用 MaskedTextbox,您可以使用 .text 属性。例如:

private void btnOk_Click(object sender, EventArgs e)
{
    if ( myMaskedTextbox.Text.Equals(PASSWORD) )
    {
        //do something
    }         

}

If using a MaskedTextbox you can use the .text property. For example:

private void btnOk_Click(object sender, EventArgs e)
{
    if ( myMaskedTextbox.Text.Equals(PASSWORD) )
    {
        //do something
    }         

}
不忘初心 2024-08-29 23:59:11

我使用下面的代码来获取PasswordBox的长度

PasswordVariableName.Password.Length

它肯定会在wp8上工作

I use below code to get the length of PasswordBox

PasswordVariableName.Password.Length

It will certainly work on wp8

极度宠爱 2024-08-29 23:59:11

您必须为您的密码框指定一个名称。

<PasswordBox Name="pwdBox"/>

然后,您可以使用 .xaml.cs 文件中的纯文本形式访问密码。

var plainPassword = pwdBox.password;

我建议您阅读 this 回答类似的问题,您将了解为什么不能将此属性值存储在任何变量中。

但是,我在文档中找到了有关 SecureString 的信息。

当您获取密码属性值时,您将密码以纯文本形式公开在内存中。为了避免这种潜在的安全风险,请使用 SecurePassword 属性以 SecureString 形式获取密码。

此引用的来源

如果我错了,请纠正我。

问候。
简。

You have to give a name to your PasswordBox.

<PasswordBox Name="pwdBox"/>

Then you can access the password as plain text in the .xaml.cs file by using

var plainPassword = pwdBox.password;

I suggest you read this answer to a similar question where you get to know why you mustn't store this property value in any variable.

However, I found in documentation information about SecureString.

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.

source of this quotation

Correct me if I am wrong.

Greetings.
Jan.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文