Search code examples
c#wpf

implementing a login view


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?


Solution

  • 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