Authenticating a user

509 Views Asked by At

I've looked through multiple solutions on here and the internet on how to solve this but I can't seem to get it. I'm just trying to make a simple login for an application I'm developing. Eventually I'll have it connected to an SQL database on a server but forget that right now. Here's the code:

<Controls:MetroWindow x:Class="ScotiaPlayTrade.Wpf.Application.LoginWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ff="clr-namespace:ScotiaPlayTrade.Wpf.Application"              
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    xmlns:System="clr-namespace:System;assembly=mscorlib" Title="Authentication"
    Height="400" Width="600" WindowStartupLocation="CenterScreen" TitleForeground="#999988"
    ResizeMode="NoResize" WindowStyle="None" WindowState="Normal" ShowMaxRestoreButton="False">

<Grid>
    <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

        <!--Username Layout-->
    <TextBlock 
         Margin="10,10,10,10" 
        Grid.Column="0" 
        Grid.Row="0">
        User Name
    </TextBlock>
    <TextBox 
         Margin="10,10,10,10" 
        x:Name="userName" 
        Grid.Column="5" 
        Grid.Row="0" 
        Text="{x:Static System:Environment.UserName}"
        IsReadOnly="True">
    </TextBox>

    <!--Password Layout-->
    <TextBlock 
         Margin="10,10,10,10" 
        Grid.Column="0" 
        Grid.Row="1">
        Password
    </TextBlock>

    <PasswordBox
        Margin="10,10,10,10" 
        Width="200" 
        x:Name="PasswordBox" 
        ff:PasswordBoxAssistant.BindPassword="true"  x:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        Grid.Column="1" 
        Grid.Row="1">
    </PasswordBox >

    <!--Buttons Layout-->
    <StackPanel Margin="3,3,3,3" Orientation="Horizontal" Grid.Column="1" Grid.Row="2">
        <Button 
            Margin="10,10,10,10" 
            Click="Login_Click" 
            IsDefault="True">
            Login
        </Button>
        <Button 
            Margin="10,10,10,10" 
            Click="Exit_Click" 
            IsCancel="True">
            Exit
        </Button>

        <Button 
            Margin="10,10,10,10" 
            Click="AddNewUser_Click" 
            IsCancel="True">
            New User
        </Button>

    </StackPanel>
</Grid>

That's my LoginWindow.xaml and here's my cs code for the window.

public partial class LoginWindow
{
    public LoginWindow()
    {
        InitializeComponent();
    }

    //Clicking Login Button
    private void Login_Click(object sender, RoutedEventArgs e)
    {
        //Check that password field is not null
        if (PasswordBox != null)
        {
            //Check that username matches password, if it matches then open main window 

            //Launch the main window after authentication is complete
            MainWindow myMainWindow = new MainWindow();
            myMainWindow.Show();

            //Close the login screen
            Close();
        }
        else
        {
            MessageBox.Show("Password field cannot be empty!");

        }


    }

    //Clicking Exit Button
    private void Exit_Click(object sender, RoutedEventArgs e)
    {
        Close();

    }

    //Clicking Exit Button
    private void AddNewUser_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Not yet implemented");

    }
}

I've tried everything with passwordbox it keeps giving me an error that WPF doesn't support it, things like PasswordBoxAssistant, and Helper. Would appreciate any help. Here are the error messages: Error 1 PasswordBoxAssistant is not supported in a Windows Presentation Foundation (WPF) project.
Error 5 The property 'PasswordBoxAssistant.BindPassword' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml'. Line 50 Position 13.

Ok so now I made a class called PasswordValidation and added the PasswordBoxAssistant code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ScotiaPlayTrade.Wpf.Application
{
    public static class PasswordBoxAssistant
    {
        public static readonly DependencyProperty BoundPassword =
            DependencyProperty.RegisterAttached("BoundPassword", typeof(string),       typeof(PasswordBoxAssistant), new PropertyMetadata(string.Empty, OnBoundPasswordChanged));

        public static readonly DependencyProperty BindPassword = DependencyProperty.RegisterAttached(
            "BindPassword", typeof(bool), typeof(PasswordBoxAssistant), new PropertyMetadata(false, OnBindPasswordChanged));

        private static readonly DependencyProperty UpdatingPassword =
            DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordBoxAssistant), new PropertyMetadata(false));

        private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox box = d as PasswordBox;

            // only handle this event when the property is attached to a PasswordBox
            // and when the BindPassword attached property has been set to true
            if (d == null || !GetBindPassword(d))
            {
                return;
            }

            // avoid recursive updating by ignoring the box's changed event
            box.PasswordChanged -= HandlePasswordChanged;

            string newPassword = (string)e.NewValue;

            if (!GetUpdatingPassword(box))
            {
                box.Password = newPassword;
            }

            box.PasswordChanged += HandlePasswordChanged;
        }

        private static void OnBindPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
        {
            // when the BindPassword attached property is set on a PasswordBox,
            // start listening to its PasswordChanged event

            PasswordBox box = dp as PasswordBox;

            if (box == null)
            {
                return;
            }

            bool wasBound = (bool)(e.OldValue);
            bool needToBind = (bool)(e.NewValue);

            if (wasBound)
            {
                box.PasswordChanged -= HandlePasswordChanged;
            }

            if (needToBind)
            {
                box.PasswordChanged += HandlePasswordChanged;
            }
        }

        private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox box = sender as PasswordBox;

            // set a flag to indicate that we're updating the password
            SetUpdatingPassword(box, true);
            // push the new password into the BoundPassword property
            SetBoundPassword(box, box.Password);
            SetUpdatingPassword(box, false);
        }

        public static void SetBindPassword(DependencyObject dp, bool value)
        {
            dp.SetValue(BindPassword, value);
        }

        public static bool GetBindPassword(DependencyObject dp)
        {
            return (bool)dp.GetValue(BindPassword);
        }

        public static string GetBoundPassword(DependencyObject dp)
        {
            return (string)dp.GetValue(BoundPassword);
        }

        public static void SetBoundPassword(DependencyObject dp, string value)
        {
            dp.SetValue(BoundPassword, value);
        }

        private static bool GetUpdatingPassword(DependencyObject dp)
        {
            return (bool)dp.GetValue(UpdatingPassword);
        }

        private static void SetUpdatingPassword(DependencyObject dp, bool value)
        {
            dp.SetValue(UpdatingPassword, value);
        }
    }

}

The following are the errors I get: Error 1 The name "PasswordBoxAssistant" does not exist in the namespace "clr-namespace:ScotiaPlayTrade.Wpf.Application". 50 13
Error 2 PasswordBoxAssistant is not supported in a Windows Presentation Foundation (WPF) project. 50 58
Error 3 The attachable property 'BindPassword' was not found in type 'PasswordBoxAssistant'. 50 13

0

There are 0 best solutions below