Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
My Question is how I can save usernames and passworts in a .csv data and by a input of the user check if there is a passwort and username in the .csv data like the one that the user just has entered right now? In C#(WPF, Visual studio)!
Posted
Comments
Sergey Alexandrovich Kryukov 3-Nov-14 17:28pm    
First, and foremost, you should not save any passwords. They are not needed for authentication and are not stored, it would be unsafe. Instead, cryptographic hash functions of the passwords is stored.
—SA
SpengergasseBiomedicin 4-Nov-14 5:26am    
thx

1 solution

Hi,

Lets suppose this is your class:
C#
public class Users
{
    public int ID { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }

    public Users(int id,string username, string email,string password)
    {
       ID = id;
       Email = email;
       UserName = username;
       Password = password;
    }
}

Now use this function to compare the values that are entered by the user.
C#
public IEnumerable<users> ReadCSV(string fileName)
{
 // Change the file extension here to make sure it's a .csv file.
 string[] lines = File.ReadAllLines(System.IO.Path.ChangeExtension(fileName, ".csv"));

   // lines.Select allows project each line as a User. 
   // This will give an IEnumerable<users> back.
   return lines.Select(line =>
   {
       string[] data = line.Split(';');
       //return a user with the data in order.
       return new Users(Convert.ToInt32(data[0]), data[1], data[2], data[3]);
    });
}
</users></users>


Now you traverse this and match that if the user name and password exists.

I am not sure whether this is the right approach or not.

Also, I agree with Sergey. And in case you want to save password by using hash functions, you can change the code as per your requirement.

Regards,
Praneet
 
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