2015-08-19 47 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