Click here to Skip to main content
15,902,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to compare the username and password entered up against two arrays one for the username and one for the password. That hasn't work. Also, I have tried using a dictionary but no luck with that.

C#
namespace PracticeLogin
{
    public partial class PasswordPracticeForm : Form
    {
       /* Dictionary<string, string> Credentials = new Dictionary<string, string>
        {
            {"joseph", "password1"},
            ("william", "password2"),
        };*/ //initially tried the above to test the input user and pass against

        /* also tried the following
         * var[] userArr = { "joseph", "william" };
         * var[] passArr = {"password1", password2"};
         * */
        static private Authenticator auth = new Authenticator();

        public PasswordPracticeForm()
        {
            InitializeComponent();

            InitializeMyControl(); //see line 53-63

        }



        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


            var username = txtBoxUserName.ToString();
            var password = txtPassword.ToString();

            var isvalid = auth.ValidateCredentials(username, password);
            if (isvalid)
            {
                MessageBox.Show("You are authenticated!");
            }
            else
                MessageBox.Show("invalid login information!");

        }

        private void InitializeMyControl() // refer to line 25. this is supposed to make each character of the password display as and asterisk. 
            //instead only shows asterisk in first spot then just shows every character of  the pass instead. retrieved this code from 
        //https://msdn.microsoft.com/en-us/library/aa983584(v=vs.71).aspx but it was not clear on where to put it.  
        {
            //set to no text.
            txtPassword.Text = "";
            //set password characters as asterisk
            txtPassword.Text = "*";
            //password length

            txtPassword.MaxLength = 25;

        }


    }
}
class Authenticator
{
    private Dictionary<string, string> Credentials = new Dictionary<string, string>();
    public Authenticator()
    {
        //username and password
        Credentials.Add("joseph", "password1");
        Credentials.Add("williams", "password");
    }

    public bool ValidateCredentials(string username, string password)
    {
        return Credentials.Any(entry => entry.Key == username && entry.Value == password);
    }
}


What I have tried:

I have tried different things I have found doing searches on google and other sites such as MSDN, Stackoverflow, Here, a few others and still no luck.

Thank you for you help.

Also, this is not related to school work or any job related task. I am a disabled Navy veteran and just trying to think of different things to do to gain more experience. The last course I took on C# was in 2014. It will be about a year before I am able to take any more.

Thank you again.
Posted
Updated 12-Apr-16 0:00am

The problem was how you read the value of the textbox, you use the Text property, not ToString.

As suggested it would be advisable to get a book that will at least cover the basics of handling controls etc.

C#
private void button1_Click(object sender, EventArgs e)
{
    // Use the Text property to access the text the user has entered
    // Converting the control itself to a string (txtBoxUserName.ToString()) returns the
    // type of the conrtol as text, not what the user has entered into it
    var username = txtBoxUserName.Text;
    var password = txtPassword.Text;

    var isvalid = auth.ValidateCredentials(username, password);
    if (isvalid)
    {
        MessageBox.Show("You are authenticated!");
    }
    else
        MessageBox.Show("invalid login information!");
}

private void InitializeMyControl()
{
    // to make a password box set what the password character is
    txtPassword.PasswordChar = '*'; 
    txtPassword.MaxLength = 25;
}
 
Share this answer
 
Seriously, get a book! You don't need to use courses - although they are a real help - but anything with a structure to it is going to be a lot better than trying to code random tasks and getting nowhere. Wrox and Addison Wesley both do some very good ones on C#, which will take you through from beginner to coder (though you will need experience on top of that).
C#
Dictionary<string, string> userData = new Dictionary<string, string>();
userData["joseph"] = "password1";
userData["william"] = "password2";
string userinputName = "joseph";
string userInputPassword = "password1";
if (userData.ContainsKey(userinputName))
    {
    if (userData[userinputName] == userInputPassword)
        {
        Console.WriteLine("OK");
        }
    else
        {
        Console.WriteLine("Failed to match");
        }
    }
else
    {
    Console.WriteLine("Unknown user");
    }

It's not quite what I would do - I'd probably create a User class which contained the name and (hashed) password - but it should do what you want!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900