0
votes

I am currently using the following attached behaviour on a PasswordBox in a WPF application. I am able to set the password using the UI, I encrypt this value and store in a database. When loading the view model and setting the SecureString property which is bound to the PasswordBox the password field appears empty. (even though the password is loaded successfully).

What would I need to change in order for the PasswordBox to appear as filled when setting the value from the ViewModel?

I have added the code used in the XAML and in the ViewModel. The LoadPassword() method returns a SecureString.

Behaviour

/// <summary>
/// Provides additional properties to use against PasswordBox controls.
/// </summary>
/// <remarks>
/// Sometimes controls have properties that don't support binding. PasswordBox is one of these, where Microsoft's implementation does not support binding against the entered
/// password (for 'security purposes').
/// 
/// This class provides an 'attached property' which allows us to bridge the gap between the view and view model.
/// </remarks>
public static class Password
{
    #region Dependency properties

    /// <summary>
    /// SecurePassword property.
    /// </summary>
    public static readonly DependencyProperty SecurePasswordProperty = DependencyProperty.RegisterAttached("SecurePassword", typeof(SecureString), typeof(Password), new FrameworkPropertyMetadata(OnSecurePasswordChanged));

    /// <summary>
    /// HasSecurePassword property.
    /// </summary>
    private static readonly DependencyProperty HasSecurePasswordProperty = DependencyProperty.RegisterAttached("HasSecurePassword", typeof(bool), typeof(Password));

    #endregion

    #region Methods

    /// <summary>
    /// Sets the value of the SecurePassword property.
    /// </summary>
    /// <param name="dependencyObject">The object to set the property for.</param>
    /// <param name="value">The value to set.</param>
    public static void SetSecurePassword(DependencyObject dependencyObject, SecureString value)
    {
        if (dependencyObject == null)
            throw new ArgumentNullException(nameof(dependencyObject));

        dependencyObject.SetValue(Password.SecurePasswordProperty, value);
    }

    /// <summary>
    /// Gets the value of the SecurePassword property.
    /// </summary>
    /// <param name="dependencyObject">The object to get the property for.</param>
    /// <returns>The current value.</returns>
    public static SecureString GetSecurePassword(DependencyObject dependencyObject)
    {
        if (dependencyObject == null)
            throw new ArgumentNullException(nameof(dependencyObject));

        return (SecureString)dependencyObject.GetValue(Password.SecurePasswordProperty);
    }

    #endregion

    /// <summary>
    /// Handles the SecurePassword value changing.
    /// </summary>
    private static void OnSecurePasswordChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        PasswordBox password = dependencyObject as PasswordBox;

        bool? isRegistered = (bool?)password?.GetValue(Password.HasSecurePasswordProperty);
        if (isRegistered == false)
        {
            // register with the PasswordBox's PasswordChanged event so that we can keep updated with the latest value entered by the user
            password.PasswordChanged += (s, ee) => SetSecurePassword(password, password.SecurePassword);
            password.SetValue(Password.HasSecurePasswordProperty, true);
        }
    }
}

XAML

<PasswordBox Grid.Column="1" Grid.Row="2" behaviours:Password.SecurePassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="2"/>

ViewModel

public SecureString Password
{
    get { return this._password; }
    set { base.SetProperty(ref _password, value); }
}

public SettingsViewModel()
{
    this.Password = LoadPassword();
}
1
Can you include the PasswordBox XAML and ViewModel code for the property?Glen Thomas
@GlenThomas I have added the further code samples, which show the bigger picture. I should have included them originally, sorry about that.seanzi

1 Answers

2
votes

You simply can't set the SecurePassword. It is a readOnly Property. If you want to fill the PasswordBox, you have to set the Unsecure String Password Property.