简体   繁体   English

设置PasswordBox的初始值

[英]Setting initial value of PasswordBox

I'm wondering if this is at all possible with all the security involved with the PasswordBox control: 我想知道PasswordBox控件所涉及的所有安全性是否完全有可能:

I have a XAML form (C#/WPF) where users will configure a database access. 我有一个XAML表单(C#/ WPF),用户可以在其中配置数据库访问权限。 In that form i'm using a PasswordBox to get the SQL Server user password. 以这种形式,我使用了一个PasswordBox来获取SQL Server用户密码。

Since this data is saved to disk for future use (in a pasword protected SQL Server CE database file), while on first run there is no password set, if the user comes back and needs to edit the SQL connection for some reason, then there could be a password kept from the previous configuration (unless he used Windows Authentication rather than SQL User Authentication) 由于此数据已保存到磁盘以供将来使用(在受密码保护的SQL Server CE数据库文件中),因此在首次运行时,没有设置密码,如果用户回来并由于某种原因需要编辑SQL连接,则存在可以是先前配置保留的密码(除非他使用Windows身份验证而不是SQL用户身份验证)

So I want to show an empty PasswordBox on the first run but if there's a password set already, when the user returns I want to show X number of '*' (to give indication that there is a password in place. 因此,我想在第一次运行时显示一个空的PasswordBox,但是如果已经设置了密码,则当用户返回时,我想显示X号“ *”(以指示已设置了密码)。

Since PasswordBox.Password isn't bindable, I can only choose to always show it empty or always show a fixed number of '*' (by setting a default Password that doesn't actually represent the real password). 由于PasswordBox.Password不可绑定,因此我只能选择始终将其显示为空或始终显示固定数量的“ *”(通过设置实际上不代表真实密码的默认密码)。

Is there any alternative (besides something like the PasswordBox Helper that injects binding of course - i'd rather not go that path as there might be a reason i haven't considered for MS to choose not to make it bindable even to a SecureString)? 是否有其他选择(当然,除了诸如PasswordBox Helper之类的注入绑定的东西外-我宁愿不走那条路,因为可能出于某种原因我没有考虑让MS选择不使其不绑定到什至SecureString)。 ?

You can read the Password from the file. 您可以从文件中读取密码。

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

So when the application is open for the first time and there would not be any password in the file it would show the empty PasswordBox. 因此,当应用程序首次打开且文件中没有任何密码时,它将显示一个空的PasswordBox。 And again when the password has already been set by the user the Password will be found in the file and it would get loaded in the PasswordBox. 再次,当用户已经设置了密码时,将在文件中找到该密码,并将其加载到PasswordBox中。

You can have this behavior for PasswordBox to enable binding in MVVM. 您可以使PasswordBox具有此行为,以在MVVM中启用绑定。

PasswordBoxBehavior.cs 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()
    {
    }
}

Use 采用

<PasswordBox>
    <i:Interaction.Behaviors>
        <bh:PasswordBoxBehavior 
            Text="{Binding UserPassword}"
            ResetPassword="{Binding IsResetPassword}" />
    </i:Interaction.Behaviors>
</PasswordBox>

where 哪里

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:bh="clr-namespace:<some namespace>;assembly=<some assembly>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM