2015-08-19 44 views
1

的初始值,我想知道,如果这是所有参与的PasswordBox控制的安全性在所有可能的:设置PasswordBox

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

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

所以我想在第一次运行时显示一个空的PasswordBox,但如果有密码已设置,用户返回我想显示X数字'*'(表示有密码到位)

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

有没有其他的选择(除了像PasswordBox Helper那样注入绑定的东西 - 我宁愿不走那条路,因为可能有一个理由我没有考虑MS让MS选择不要绑定它到一个SecureString)?

+0

@ faflo10:是的,我分离了视图,视图模型和模型,但当然可以打破这些规则,如果绝对需要。从密码箱中读取的密码保存在我的模型中,而密码箱位于视图 – 537mfb

+1

您可以从文件中读取并编写代码,说明'PasswordBox.Password =“ReadPassword”;' –

+0

' PasswordBox'并不适合'MVVM',但您可以编写附加行为来设置['PasswordBox.SecurePassword'](https://msdn.microsoft.com/en-us/library/system.windows.controls.passwordbox .securepassword.aspx)(这是'SecureString')。这种行为必须能够访问现有的密码(在某些User类中也是'SecureString'),或者输入一些* fake *密码(但是你可以简单地使用'PasswordBox.Password')。 'User.IsLogged'已设置。 – Sinatr

回答

1

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

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

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

+0

这是可行的 - 通过在模型中使用从视图模型触发的事件来更改密码。该视图通过设置PasswordBox的值来响应该事件,并且MVVM保持不变 - 很好的答案。还应该使用SecureString保持密码在模型中,而不是普通的字符串,以保持安全并且不会否定PasswordBox的安全特性 – 537mfb

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>" 
+0

这基本上是我试图避免的在我的问题中提到的PasswordBox助手类。没有?我看, – 537mfb

+0

,可能是你尝试发布订阅模式。 https://msdn.microsoft.com/en-us/library/windows/apps/xx130639.aspx – daniel