gpt4 book ai didi

wpf - 从 PasswordBox 获取密码

转载 作者:行者123 更新时间:2023-12-03 10:38:44 28 4
gpt4 key购买 nike

我在这里找到了一些关于这个问题的信息,但不知何故我并没有真正得到它;-) 从我所读到的内容来看,由于安全原因,PasswordBox 的密码不能绑定(bind)到属性,即保留普通密码在内存中。

我的模型包含这个:

private SecureString password;
public SecureString Password {
get { return password; }
set { password = value; }
}

虽然不支持将数据绑定(bind)到 PasswordBox,但 Microsoft 必须知道如何从 PasswordBox 获取密码并以安全的方式使用它,嗯?

什么是合适且相对简单的方法?

最佳答案

为此我写了一个UserControl使用可绑定(bind)密码 - SecureString .此代码UserControl好像:

代码隐藏:

public partial class BindablePasswordBox : UserControl
{
public static readonly DependencyProperty SecurePasswordProperty = DependencyProperty.Register(
"SecurePassword", typeof(SecureString), typeof(BindablePasswordBox), new PropertyMetadata(default(SecureString)));

public SecureString SecurePassword
{
get { return (SecureString)GetValue(SecurePasswordProperty); }
set { SetValue(SecurePasswordProperty, value); }
}

public BindablePasswordBox()
{
InitializeComponent();
}

private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
{
SecurePassword = ((PasswordBox)sender).SecurePassword;
}

private void BindablePasswordBox_OnGotFocus(object sender, RoutedEventArgs e)
{
passwordBox.Focus();
}
}

XAML:
<UserControl x:Class="Sol.Controls.BindablePasswordBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
GotFocus="BindablePasswordBox_OnGotFocus">
<PasswordBox x:Name="passwordBox" PasswordChanged="PasswordBox_OnPasswordChanged"/>
</UserControl>

关于wpf - 从 PasswordBox 获取密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com