我想知道,对于PasswordBox控件所涉及的所有安全性,这是否是可能的:
我有一个XAML表单(C#/WPF),用户将在其中配置数据库访问。在这种形式下,我使用一个PasswordBox来获取Server用户密码。
由于这些数据保存到磁盘以供将来使用(在pasword保护的Server数据库文件中),在第一次运行时没有设置密码,如果用户返回并需要编辑SQL连接,那么可能会有一个密码保存在以前的配置中(除非他使用Windows身份验证而不是SQL用户身份验证)
因此,我希望在第一次运行时显示一个空的PasswordBox,但是如果已经设置了密码,当用户返回时,我希望显示X号'*‘(指示密码已经到位。
因为PasswordBox.Password是不可绑定的,所以我只能选择总是显示它为空,或者总是显示一个固定数目的“*”(通过设置一个默认密码,这个密码实际上并不代表真正的密码)。
有没有其他的选择(当然,除了注入绑定的PasswordBox帮助程序-我不想走这条路,因为我可能没有考虑到MS选择不将它绑定到SecureString上)?
发布于 2015-08-19 17:29:24
您可以从文件中读取密码。
//Storing the Password in String.
string pwd = "Password Read from the file";
PasswordBox.Password = pwd;
因此,当应用程序第一次打开并且文件中没有任何密码时,它将显示空的PasswordBox。同样,当用户已经设置了密码时,将在文件中找到密码,并将其加载到PasswordBox中。
发布于 2015-08-19 00:18:40
您可以为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>"
https://stackoverflow.com/questions/32089243
复制