【问题标题】:How to match user credentials against text file contents in C#如何将用户凭据与 C# 中的文本文件内容进行匹配
【发布时间】:2015-11-29 04:26:34
【问题描述】:

我正在尝试创建一个登录方法,用于根据名为 "accounts.txt" 的文本文件检查用户凭据,该文本文件已经包含 "Bob password" 等字符串登录它。

login 方法参数类似于 - “login”是command,用户名(例如“Bob”)是param1,密码(例如“password*44”)是param2

当我运行命令行参数时,例如 “登录 Bob 密码” 该方法应该逐行读取文件的内容,直到找到匹配项并返回“登录 Bob 成功”。否则它会说“无效的用户名/密码”

我不知道该怎么做,感谢任何提示。这是我当前的代码

protected void login(string command, string param1, string param2) // string command "login" string username, string password
{
     // login command
     // Needs to check accounts.txt file for username and password.
     // if username and password exists, provide logon message
     // if username and/ or password does not exist, provide failed logon message.
     // IN MEMORY store current login username and password

     //checks accounts.txt file if username already exists and if there is a match

     if (not sure what arg would go here)
     {
         string path = "C:\\Files\\accounts.txt";
         Console.WriteLine("username and password match", path);
     }
     else
     {
         Console.WriteLine("Login Failed: invaild username or password");
         string path2 = "C:\\Files\\audit.txt";
         using (StreamWriter sw2 = File.AppendText(path2))
         {
              sw2.WriteLine("Login Failed: Invaild username or password");
         }
         throw new FileNotFoundException();
 }

【问题讨论】:

  • 您是否真的认为这是一个安全模型?任何人都可以轻松地从文件中读取密码。考虑为此使用 SQL Server LocalDB,它更容易且更安全。
  • @cFrozenDeath 是对的。您至少应该为您的应用添加一些安全层,以保护它免受恶作剧用户的侵害。
  • 编写此代码只是为了练习以了解更多信息并获得乐趣,但我感谢您的建议!

标签: c# if-statement text command-line-arguments


【解决方案1】:

你的方法看起来像

protected void login(string command, string param1, string param2) // string command "login" string username, string password
{
    if (command == "login")
    {
        var logins = File.ReadAllLines(@"C:\\Files\\accounts.txt");
        if (logins.Any(l => l == string.Format("{0} {1}", param1, param2)))
            Console.WriteLine("login {0} successful", param1);
        else
        {
            //log audit details
            Console.WriteLine("invaild username/password");
        }
    }
}

【讨论】:

    【解决方案2】:

    您可以读取一个数组中的所有行,然后尝试在该数组中找到您的字符串。

    string textToFind = string.Format ("{0} {1} {2}",command,param1,param2)
    bool anyHit = File.ReadAllLines (path).Any(x => string.Compare (textToFind,x) == 0));
    

    【讨论】:

    • 使用string.Compare而不是==有什么具体原因吗?
    【解决方案3】:

    正如@Valentin 所说,您可以使用 File.ReadAllLines() 读取文本文件的所有行。

    string path = "C:\\Files\\accounts.txt";
    string[] LoginCredentials = File.ReadAllLines(path);
    for(i=0;i<LoginCredentials.Length;i++)
    {
        //Split Line (Credential[0] = Username, Credential[1] = Password)
        string[] Credential = LoginCredentials[i].Split(' ');
    
        //Check if input matches
        if(param1 == Credential[0] && param2 == Credential[1])
        {
           Console.WriteLine("username and password match", path); 
        }
    }
       // If Input never matched login failed
        Console.WriteLine("Login Failed: invaild username or password");
        string path2 = "C:\\Files\\audit.txt";
        using (StreamWriter sw2 = File.AppendText(path2))
        {
        sw2.WriteLine("Login Failed: Invaild username or password");
        }
        throw new FileNotFoundException();
    

    【讨论】:

    • 这将总是写入“登录失败:用户名或密码无效”。您还可以对代码进行其他改进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2013-03-18
    相关资源
    最近更新 更多