简体   繁体   中英

How to validate username and password from text file? | Winforms C#

First I made textbox1(for username) , textbox2(for password) and button1(check). After:

private void button1_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream(@"D:\C#\test.txt", FileMode.Open, FileAccess.Read, FileShare.None);
    StreamReader sr = new StreamReader(fs);
}

I want to check username from the first line of test.txt (equals like added from me text in textbox1) and password from the second line.

Sorry for my bad english.

You could try something like this:

private void button1_Click(object sender, EventArgs e){
     string[] lines = System.IO.File.ReadAllLines(@"D:\C#\test.txt");
     String username = lines[0];
     String password = lines[1];
}

However, it's not a very good way to store your usernames and passwords. I'm assuming you're just testing something out.

The simplest answer to your question is to read the text-file line by line. I would however strongly suggest at least seeding and hashing the password.

This is a short example utilizing seeded SHA256 hashed passwords. It's just to show the concept and should not be used as is.

    void Test()
    {
        string pwd = "Testing1234";
        string user = "username";

        StorePassword(user, pwd);

        bool result = ValidatePassword(user, pwd);
        if (result == true) Console.WriteLine("Match!!");
    }

    private void StorePassword(string username, string password)
    {
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var random = new Random();
        var salt = new string(
            Enumerable.Repeat(chars, 8)
                   .Select(s => s[random.Next(s.Length)])
                   .ToArray());

        string hash = GetHash(salt + password);
        string saltedHash = salt + ":" + hash;
        string[] credentials = new string[] { username, saltedHash };

        System.IO.File.WriteAllLines(@"D:\C#\test.txt",credentials);

    }

    bool ValidatePassword(string username, string password)
    {
        string[] content = System.IO.File.ReadAllLines(@"D:\C#\test.txt");

        if (username != content[0]) return false; //Wrong username

        string[] saltAndHash = content[1].Split(':'); //The salt will be stored att index 0 and the hash we are testing against will be stored at index 1.

        string hash = GetHash(saltAndHash[0] + password);

        if (hash == saltAndHash[1]) return true;
        else return false;

    }

    string GetHash(string input)
    {
        System.Security.Cryptography.SHA256Managed hasher = new System.Security.Cryptography.SHA256Managed();
        byte[] bytes = hasher.ComputeHash(Encoding.UTF8.GetBytes(input));

        return BitConverter.ToString(bytes).Replace("-", "");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM