3 Replies - 2354 Views - Last Post: 21 March 2011 - 09:57 AM Rate Topic: -----

#1 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Validating username and password

Posted 20 March 2011 - 10:14 AM


I've created a class and entered three users.. see below:

class Employee
        public string userName;   // comment here, privacy modifiers (encapsulation)
        private string _password;  // underscore for private class variable

        public Employee(string name, string passcode)
            userName = name;
            _password = passcode;

And these are the users, located on another class entitled 'program'

Employee userNameOne = new Employee("Joe", "Bloggs");
            Employee userNameTwo = new Employee("Jack", "Daniel");
            Employee userNameThree = new Employee("Store", "Manager");

How can I write code that can validate these credentials when a username logs in?
Basically, what I want to do is very simple.. The program will start asking the user to enter their username, then their password and the program will validate these with the details above. I've tried using 'if' statements but the code crashes and I get mad :(

I've tried a bool function within the 'Employee' class:

public bool IsAuthenticated(string user, string passcode)
            bool authenticated = false;

                Employee entry = new Employee(user, passcode);


But as you can see I've gone wayward.
Can anyone help?

Is This A Good Question/Topic? 0
  • +

Replies To: Validating username and password

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Validating username and password

Posted 20 March 2011 - 10:23 AM

Something like this:

public bool VerifyCredentials(string username, string password)
    //You have to load the user from somewhere.
    //Either a database, a file, or even a hard coded new User();
    User myUser = GetSomeUserFromSomewhere(); 
    if(myUser.Password == password && myUser.Username == username)
        return true;
        return false;

Now, for this to work, your user class needs a public attribute or how we call it in the C# world, a Property.

public class User
    public string Username { get; set; }
    public string Password { get; set; }

See auto-implemented properties.

This post has been edited by Sergio Tapia: 20 March 2011 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Freelance developer
  • member icon

Reputation: 6219
  • View blog
  • Posts: 13,724
  • Joined: 02-June 10

Re: Validating username and password

Posted 20 March 2011 - 10:25 AM

This is a really bad way to do user log on but...

You don't seem to have any code here that even tries to check the entered information against the known information. There doesn't seem to be any good faith effort to code for the problem; just a 5 minute start on making an Employee class with a couple properties.

You need to loop through all of your Employees.
When you find the one with the matching name, check if the password matches.
Was This Post Helpful? 1
  • +
  • -

#4 frostyraver  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 195
  • Joined: 21-April 09

Re: Validating username and password

Posted 21 March 2011 - 09:57 AM

What you could do is have an array of usernames and passwords.

usernameEnterd = inputbox.show("Enter a username:")
passwordEnterd = inputbox.show("Enter a password:")

for (int i = 0; i < UserNameList[].Max; i++)
	if (usernameEnterd == UserNameList[i] && PasswordEnterd == PasswordList[i])
		MessageBox.show("Welcome " + UserNameList[i] + "!")

Then have something like this to check you can always add in a Failed Login routine where if the user has failed 3 times then lock the program or something.

Get and set methods are ok, but not really needed.

It's 1am and I did it off the top of my head, so there IS errors with it, like that break; but you get the idea for all of this, you could even have a switch statement grabbing the username or something.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1