0

I want to have a login view in my WPF application.

I've developed a view for my login including the following code:

<Grid>
        <StackPanel Margin="10">
            <Label>Username:</Label>
            <TextBox Height="25" Margin="0,0,0,0" Text="{Binding Username}"/>
            <Label>Password:</Label>
            <PasswordBox Height="25" Margin="0,20,0,0" Name="txtPassword"/>
            <Button DockPanel.Dock="Bottom" VerticalAlignment="Bottom" Margin="10" TabIndex="99"
                Command="{Binding Path=LoginCommand}" CommandParameter="{Binding ElementName=txtPassword, Path=Password}">Login</Button>
        </StackPanel>
    </Grid>

in my code side I have

public string Username
        {
            get { return username; }
            set { username = value; loginCmd.CanExecute(value); }
        }

        ICommand loginCmd = new PasswordCommand();
        public ICommand LoginCommand => loginCmd;

private class PasswordCommand : ICommand
        {
            public event EventHandler CanExecuteChanged;
            private string username;

            public AuthenticatedClient AuthenticatedClient;

            public bool CanExecute(object parameter)
            {
                username = parameter as string;
                return true;// !string.IsNullOrEmpty(username);
            }

            public void Execute(object parameter)
            {
                System.Windows.MessageBox.Show(parameter as string);

                if (AuthenticatedClient == null)
                {
                    AuthenticatedClient = new AuthenticatedClient(ConfigurationManager.AppSettings["AuthEndpoint"],
                                                              username,
                                                              parameter as string,
                                                              ConfigurationManager.AppSettings["ClientId"],
                                                              ConfigurationManager.AppSettings["ClientSecret"]);
                }
}

I need the Password to be sent to my endpoint in order to authenticate the user but the value comes up is blank. How can I fix my code?

  • 1
    I cant see a password variable over here and its not passed to the AuthenticatedClient elsewhere.... – David Oct 07 '19 at 08:23
  • https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.passwordbox.securepassword?view=netframework-4.8, here is the reason... Binding Password exposes the password in memory. Use securepassword instead – Nawed Nabi Zada Oct 07 '19 at 08:40
  • For usage examples: https://learn.microsoft.com/en-us/dotnet/api/system.security.securestring?view=netframework-4.8 – Nawed Nabi Zada Oct 07 '19 at 08:42
  • https://stackoverflow.com/questions/1483892/how-to-bind-to-a-passwordbox-in-mvvm this may help you – FakeCaleb Oct 07 '19 at 10:22

2 Answers2

0

You can't bind to the Password or SecurePassword property like this, but if you just want to be able to extract the password in clear text in your command, you could bind to the control itself:

public void Execute(object parameter)
{
    PasswordBox pb = parameter as PasswordBox;
    System.Windows.MessageBox.Show(pb.Password);

    //...
}

XAML:

CommandParameter="{Binding ElementName=txtPassword}">
mm8
  • 163,881
  • 10
  • 57
  • 88
0

the best answer I found in the Internet was saying:

<Grid>
        <StackPanel Margin="10">
            <Label>Username:</Label>
            <TextBox Height="25" Margin="0,0,0,0" Text="{Binding Username}" Name="txtUsername"/>
            <Label>Password:</Label>
            <PasswordBox Height="25" Margin="0,-20,0,0" Name="txtPassword"
                         ff:PasswordBoxAssistant.BindPassword="true"  ff:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <Button DockPanel.Dock="Bottom" VerticalAlignment="Bottom" Margin="10" TabIndex="99" Height="25" Content="Login"
                Command="{Binding Path=LoginCommand}">
            </Button>
        </StackPanel>
</Grid>

therefore we can bind the Password

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40