![](/img/trans.png)
[英]How to create a group of unique users in C# console application with name, username and password where username is unique for all users?
[英]How to create a login in console where I authenticate using an username and password stored in a file c#
因此,我需要在Visual Studio控制台中創建一個登錄機制,以使用存儲在文件中的用戶名和密碼對客戶端進行身份驗證,這是文件的內容:
1; user1; mail1@gmail.com; username1; pass1 2; user2; mail2@gmail.com; username2; pass2 3; user3; mail3@gmail.com; username3; pass3 4; user4; mail4@gmail.com; username4 ; pass4 5; user5; mail5@gmail.com; username5; pass5
我以前從未使用過C#。
先感謝您!
我已經嘗試了以下代碼,但是卻收到一個異常null,說arr []為null:
string[] lines = File.ReadAllLines(@"c:/User.txt");
string[][] arr = new string[lines.Length][];
string username, password;
string fileuser = lines[3];
string filepassw = lines[4];
int userpos = 3;
int passwpos = 4;
bool isValideUser = false;
for (int x = 3; x >= 1; x--)
{
Console.WriteLine("You have " + x + " attempt/s.");
Console.Write("Enter Username>> ");
username = Console.ReadLine();
Console.Write("Enter Password>> ");
password = Console.ReadLine();
for (int row = 0; row < 3; row++)
{
if (username.Equals(arr[row][userpos]) && password.Equals(arr[row][passwpos]))
{
Console.WriteLine("Welcome " + lines[1] + "!");
isValideUser = true;
break;
}
}
if (!isValideUser)
{
Console.WriteLine("Invalid Input.");
if (x != 1)
{
Console.WriteLine("Please Try Again.");
Console.Write("\n");
}
else if (x.Equals(1))
{
Console.Write("Goodbye!");
break;
}
}
else
break;
}
Console.ReadKey();
您必須使用值填充arr
,我將arr
更改為類型List<string[]>
,然后使用foreach
和string.Split()
使用文件中的值填充List
:
插入的:
string[][] arr = new string[lines.Length][];
采用:
List<string[]> arr = new List<string[]>();
foreach (var item in lines)
{
arr.Add(item.Split(';'));
}
通過此修改,程序可以正常運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.