【发布时间】: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