设置PasswordBox的初始值

时间:2015-08-19 07:35:05

标签: c# wpf wpf-controls passwordbox

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

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

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

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

由于PasswordBox.Password不可绑定,我只能选择始终将其显示为空或始终显示固定数量的'*'(通过设置实际上并不代表真实密码的默认密码)。

有没有替代方案(除了像PasswordBox Helper这样注入绑定的东西当然 - 我宁愿不去那条路,因为我可能有一个原因我没有考虑让MS选择不让它可绑定甚至到一个SecureString)?

2 个答案:

答案 0 :(得分:3)

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

$sql = "select ".$st_char."
        from dbo.".$st_tab."
        where bike_id like ".$bike_id;

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

答案 1 :(得分:1)

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