【问题标题】:How to validate username and password from text file? | Winforms C#如何从文本文件中验证用户名和密码? | C#
【发布时间】:2015-03-06 18:02:48
【问题描述】:

首先我制作了 textbox1(用于用户名)、textbox2(用于密码)和 button1(检查)。 之后:

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);
}

我想检查 test.txt 第一行的用户名(等于 textbox1 中从我添加的文本)和第二行的密码。

对不起,我的英语不好。

【问题讨论】:

  • 文本文件中的数据如何?
  • 你在文本文件中使用什么作为分隔符关于分隔符.. 最好使用数据库来存储用户名密码,如果文件由; , | Tab Space etc 分隔,那么你可以利用string.Split() 方法
  • @LongSmith BufferedReader 和 JTextFields 是 Java 概念,在 .NET 中不可用

标签: c# winforms


【解决方案1】:

你可以试试这样的:

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];
}

但是,这不是存储用户名和密码的好方法。我假设你只是在测试一些东西。

【讨论】:

    【解决方案2】:

    您问题的最简单答案是逐行阅读文本文件。但是,我强烈建议至少对密码进行播种和哈希处理。

    这是一个使用种子 SHA256 散列密码的简短示例。这只是为了展示概念,不应该按原样使用。

        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("-", "");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      相关资源
      最近更新 更多