2

I have found several information on this issue here at SO but somehow I'm not really getting it ;-) From what I have read, the password of a PasswordBox cannot be bound to a property due to security reasons, i.e. keeping the plain password in memory.

My model contains this:

private SecureString password;
public SecureString Password {
  get { return password; }
  set { password = value; }
}

Though data binding to a PasswordBox is not supported, Microsoft must have some idea how to get the password from the PasswordBox and use it in a secure way, eh?

What could be an appropriate and relatively easy way to do so?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Robert Strauch
  • 12,055
  • 24
  • 120
  • 192
  • 2
    go to this thread http://stackoverflow.com/questions/1483892/how-to-bind-to-a-passwordbox-in-mvvm. It will be helpful for you :) – CodeNotFound Nov 22 '12 at 13:17
  • 2
    You can use binding with an attached property as described in http://stackoverflow.com/questions/888466/passwordbox-binding – Niki Nov 22 '12 at 13:21
  • You can refer the thread . It might be helpful for you:) – Ramashankar Dec 03 '13 at 06:11

2 Answers2

2

Therefor I have written a UserControl with a bindable Password-SecureString. The code of this UserControl looks like:

Code-Behind:

public partial class BindablePasswordBox : UserControl
    {
        public static readonly DependencyProperty SecurePasswordProperty = DependencyProperty.Register(
           "SecurePassword", typeof(SecureString), typeof(BindablePasswordBox), new PropertyMetadata(default(SecureString)));

        public SecureString SecurePassword
        {
            get { return (SecureString)GetValue(SecurePasswordProperty); }
            set { SetValue(SecurePasswordProperty, value); }
        }

        public BindablePasswordBox()
        {
            InitializeComponent();
        }

        private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
        {
            SecurePassword = ((PasswordBox)sender).SecurePassword;
        }

        private void BindablePasswordBox_OnGotFocus(object sender, RoutedEventArgs e)
        {
            passwordBox.Focus();
        }
    }

XAML:

<UserControl x:Class="Sol.Controls.BindablePasswordBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
             GotFocus="BindablePasswordBox_OnGotFocus">
    <PasswordBox x:Name="passwordBox" PasswordChanged="PasswordBox_OnPasswordChanged"/>
</UserControl>
Tomtom
  • 9,087
  • 7
  • 52
  • 95
0
<PasswordBox Height="29" HorizontalAlignment="Left" Margin="191,136,0,0" Name="textPassword" VerticalAlignment="Top" PasswordChar="*" Width="167" />

Name of passwordbox is textPassword:

String pass = textPassword.Password;