设置PasswordBox的初始值



我想知道PasswordBox控件所涉及的所有安全性是否都是可能的:

我有一个XAML表单(c#/WPF),用户将在其中配置数据库访问。在这种形式下,我使用PasswordBox来获取SQL Server用户密码。

由于此数据保存到磁盘以供将来使用(在密码保护的SQL Server CE数据库文件中),而在第一次运行时没有设置密码,如果用户回来并且出于某种原因需要编辑SQL连接,那么可能会保留以前配置的密码(除非他使用Windows身份验证而不是SQL用户身份验证)

所以我想在第一次运行时显示一个空的PasswordBox,但是如果已经设置了密码,当用户返回时,我想显示X个'*'(以表明有一个密码)。

自PasswordBox

。密码是不可绑定的,我只能选择总是显示为空或总是显示一个固定数量的'*'(通过设置一个默认的密码,实际上并不代表真正的密码)。

是否有其他选择(除了像PasswordBox助手注入绑定当然-我宁愿不走那条路,因为可能有一个原因,我没有考虑MS选择不使其绑定甚至到SecureString)?

您可以从文件中读取密码。

//Storing the Password in String.
string pwd = "Password Read from the file";
PasswordBox.Password = pwd;

因此,当应用程序第一次打开并且文件中没有任何密码时,它将显示空PasswordBox。当用户已经设置了密码时,密码会在文件中找到,它会被加载到PasswordBox中。

您可以为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>"

相关内容

  • 没有找到相关文章

最新更新