设置密码框的初始值
Setting initial value of PasswordBox
我想知道 PasswordBox 控件涉及的所有安全性是否完全可行:
我有一个 XAML 表单 (C#/WPF),用户将在其中配置数据库访问。在那种形式下,我使用 PasswordBox 获取 SQL 服务器用户密码。
由于此数据保存到磁盘以备将来使用(在受密码保护的 SQL 服务器 CE 数据库文件中),而最初 运行 没有设置密码,如果用户回来并且出于某种原因需要编辑 SQL 连接,然后可能会保留以前配置中的密码(除非他使用 Windows 身份验证而不是 SQL 用户身份验证)
所以我想在第一个 运行 上显示一个空的密码框,但是如果已经设置了密码,当用户 returns 我想显示 X 个“*”(给表明有密码。
由于PasswordBox.Password不可绑定,我只能选择始终显示为空或始终显示固定数量的“*”(通过设置默认密码,该密码实际上并不代表真实密码).
是否有任何替代方案(除了 PasswordBox Helper 之类的东西当然可以注入绑定 - 我宁愿不走那条路,因为我可能没有考虑过让 MS 选择不使其可绑定的原因甚至是 SecureString)?
您可以为 PasswordBox 设置此行为以在 MVVM 中启用绑定。
PasswordBoxBehavior.cs
public class PasswordBoxBehavior : Behavior<PasswordBox>
{
public bool ResetPassword
{
get { return (bool)GetValue(ResetPasswordProperty); }
set { SetValue(ResetPasswordProperty, value); }
}
// Using a DependencyProperty as the backing store for ResetPassword. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ResetPasswordProperty =
DependencyProperty.Register("ResetPassword", typeof(bool), typeof(PasswordBoxBehavior), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnResetPasswordChanged));
static void OnResetPasswordChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
PasswordBoxBehavior behavior = depObj as PasswordBoxBehavior;
PasswordBox item = behavior.AssociatedObject as PasswordBox;
if (item == null)
return;
if ((bool)e.NewValue)
item.Password = string.Empty;
behavior.ResetPassword = false;
}
private bool isRoutedEventHandlerAssign;
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(PasswordBoxBehavior), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged));
static void OnTextChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
PasswordBoxBehavior behavior = depObj as PasswordBoxBehavior;
PasswordBox item = behavior.AssociatedObject as PasswordBox;
if (item == null)
return;
if (item.Password != e.NewValue as string)
{
item.Password = e.NewValue as string;
}
if (!behavior.isRoutedEventHandlerAssign)
{
item.PasswordChanged += (sender, eArg) =>
{
behavior.Text = item.Password;
};
behavior.isRoutedEventHandlerAssign = true;
}
}
public PasswordBoxBehavior()
{
}
}
使用
<PasswordBox>
<i:Interaction.Behaviors>
<bh:PasswordBoxBehavior
Text="{Binding UserPassword}"
ResetPassword="{Binding IsResetPassword}" />
</i:Interaction.Behaviors>
</PasswordBox>
哪里
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:bh="clr-namespace:<some namespace>;assembly=<some assembly>"
您可以从文件中读取密码。
//Storing the Password in String.
string pwd = "Password Read from the file";
PasswordBox.Password = pwd;
因此,当应用程序首次打开且文件中没有任何密码时,它会显示空的 PasswordBox。再次,当用户已经设置密码时,密码将在文件中找到,并将加载到 PasswordBox 中。
我想知道 PasswordBox 控件涉及的所有安全性是否完全可行:
我有一个 XAML 表单 (C#/WPF),用户将在其中配置数据库访问。在那种形式下,我使用 PasswordBox 获取 SQL 服务器用户密码。
由于此数据保存到磁盘以备将来使用(在受密码保护的 SQL 服务器 CE 数据库文件中),而最初 运行 没有设置密码,如果用户回来并且出于某种原因需要编辑 SQL 连接,然后可能会保留以前配置中的密码(除非他使用 Windows 身份验证而不是 SQL 用户身份验证)
所以我想在第一个 运行 上显示一个空的密码框,但是如果已经设置了密码,当用户 returns 我想显示 X 个“*”(给表明有密码。
由于PasswordBox.Password不可绑定,我只能选择始终显示为空或始终显示固定数量的“*”(通过设置默认密码,该密码实际上并不代表真实密码).
是否有任何替代方案(除了 PasswordBox Helper 之类的东西当然可以注入绑定 - 我宁愿不走那条路,因为我可能没有考虑过让 MS 选择不使其可绑定的原因甚至是 SecureString)?
您可以为 PasswordBox 设置此行为以在 MVVM 中启用绑定。
PasswordBoxBehavior.cs
public class PasswordBoxBehavior : Behavior<PasswordBox>
{
public bool ResetPassword
{
get { return (bool)GetValue(ResetPasswordProperty); }
set { SetValue(ResetPasswordProperty, value); }
}
// Using a DependencyProperty as the backing store for ResetPassword. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ResetPasswordProperty =
DependencyProperty.Register("ResetPassword", typeof(bool), typeof(PasswordBoxBehavior), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnResetPasswordChanged));
static void OnResetPasswordChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
PasswordBoxBehavior behavior = depObj as PasswordBoxBehavior;
PasswordBox item = behavior.AssociatedObject as PasswordBox;
if (item == null)
return;
if ((bool)e.NewValue)
item.Password = string.Empty;
behavior.ResetPassword = false;
}
private bool isRoutedEventHandlerAssign;
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(PasswordBoxBehavior), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged));
static void OnTextChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
PasswordBoxBehavior behavior = depObj as PasswordBoxBehavior;
PasswordBox item = behavior.AssociatedObject as PasswordBox;
if (item == null)
return;
if (item.Password != e.NewValue as string)
{
item.Password = e.NewValue as string;
}
if (!behavior.isRoutedEventHandlerAssign)
{
item.PasswordChanged += (sender, eArg) =>
{
behavior.Text = item.Password;
};
behavior.isRoutedEventHandlerAssign = true;
}
}
public PasswordBoxBehavior()
{
}
}
使用
<PasswordBox>
<i:Interaction.Behaviors>
<bh:PasswordBoxBehavior
Text="{Binding UserPassword}"
ResetPassword="{Binding IsResetPassword}" />
</i:Interaction.Behaviors>
</PasswordBox>
哪里
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:bh="clr-namespace:<some namespace>;assembly=<some assembly>"
您可以从文件中读取密码。
//Storing the Password in String.
string pwd = "Password Read from the file";
PasswordBox.Password = pwd;
因此,当应用程序首次打开且文件中没有任何密码时,它会显示空的 PasswordBox。再次,当用户已经设置密码时,密码将在文件中找到,并将加载到 PasswordBox 中。