I am currently writing a login method that keeps track of a logged in user by reading the contents of credentials saved in a text file called "accounts.txt".
The method works fine up to this point however I dont understand how I could make a simple check to prevent multiple logins. I am using command line arguments to input a string that logs in a user.
Example of this when input by command line is "login tom C#password" with "login" being passed as the command parameter, "tom" as param1, and "C#password" as the param2.
Ideally the method should check as an if statement that prevents a double login until the user inputs a "logout" command after "login" has already been used by command line args.
I'm fairly new to c#, any kind of advice is appreciated
protected void login(string command, string param1, string param2) // string command "login" string username, string password
{
// login command
// Needs to check check accounts.txt file for username and password.
// if username and password exists, provide login message/else failed login
// NO two logins at the same time until logout command is provided after the first login
// checks accounts.txt file if username already exists and if there is a match, if not a prompt says login has failed
var logins = File.ReadAllLines(@"C:\\Files\\accounts.txt");
if (command == "login")
{
if (logins.Any(l => l == string.Format("{0} {1}", param1, param2)))
Console.WriteLine("Login {0} ", param1);
string path1 = "C:\\Files\\audit.txt";
using (StreamWriter sw1 = File.AppendText(path1))
{
sw1.WriteLine("User " + param1 + " logged in");
}
}
else
Console.WriteLine("Login Failed: Invaild username or password");
}
//checks if more than one user logged in
if (check for double login command)
{
Console.WriteLine("Login Failed: simultaneous login not permitted");
string path2 = "C:\\Files\\audit.txt";
using (StreamWriter sw2 = File.AppendText(path2))
{
sw2.WriteLine("Login Failed: simultaneous login not permitted");
}
}
Console.Read();
}
logout is a simple method that records if a user has logged out
protected void logout(string command, string param1)
{
// remove current username and password from memory
var logout = File.ReadAllLines(@"C:\\Files\\accounts.txt");
if (command == "logout")
{
if (logout.Any(l => l == string.Format("{0} {1}", param1, param2)))
Console.WriteLine("Logout {0} ", param1);
string path1 = "C:\\Files\\audit.txt";
using (StreamWriter sw1 = File.AppendText(path1))
{
sw1.WriteLine("User " + param1 + " logged out");
}
}
else
Console.WriteLine("Logout Failed");
Console.Read();
}
Aucun commentaire:
Enregistrer un commentaire