2015-10-18 22 views
-1

我對C#相當陌生,而且似乎無法找到我在線尋找的內容。我需要使用用戶名和密碼功能創建一個應用程序"Authenticate"。良好的憑證,如"Admin, Pa$$w0rd"等將被保存在該CSV文件中,加載到我的Authenticator.cs中的字典中,然後通過用戶在表單上的文本框中提供的任何內容進行驗證。然後在按鈕上將輸入與CSV進行比較並進行身份驗證。希望我已經夠清楚了。從CSV文件加載值到Windows Form C#字典

到目前爲止,我設法創建窗體和文本框等,以及作爲認證級(從我的控制檯應用程序,下面的代碼複製,沒有改變的形式還)

class Authenticator 
{ 
    //Created a Private Dictionary to hide contents 
    private Dictionary<string, string> Users = new Dictionary<string, string>(); 
    public Authenticator() 
    { 
     //Details - adding keys and values to dictionary 
     Users.Add("Administrator", "Pa$$w0rd"); 
     Users.Add("Admin", "password"); 
     Users.Add("root", "secure"); 
    } 

    //Public Bool to Validate dictionary data 
    public bool ValidateCredentials(string username, string password) //Focuses on the username key and password value 
    { 
     return Users.Any(entry => entry.Key == username && entry.Value == password); 
     //When entered username matches the key and password input matches the key 
     //Then bool is true 
    } 
} 

}

所以,如果有人知道:

  1. 我怎樣才能CSV數據加載到一個字典在我的課
  2. 比較輸入數據VS羅亞代理數據
  3. 製作一個日誌文件(csv),用於記錄日期時間,使用用戶名和密碼的每個"Authentication"嘗試。

很多謝謝。

+0

兩件事情:[CSVHelper](https://www.nuget.org/packages/CsvHelper/)另一種是在明文口令的存儲一個沒有沒有。 – t0mm13b

+0

以純文本形式存儲密碼?您應該考慮對此的安全影響。 – disklosr

回答

0

使用Oledb讀取csv文件。像這樣

using System; 
 
using System.Collections.Generic; 
 
using System.Linq; 
 
using System.Text; 
 
using System.Data; 
 
using System.Data.OleDb; 
 
using System.IO; 
 

 
namespace ConsoleApplication1 
 
{ 
 
    class Program 
 
    { 
 
     const string FILENAME = @"c:\temp\test.csv"; 
 
     static void Main(string[] args) 
 
     { 
 
      CSVReader reader = new CSVReader(); 
 

 
      DataSet ds = reader.ReadCSVFile(FILENAME, true); 
 
      DataTable dt = ds.Tables[0]; 
 

 
      Dictionary<string, string> dict = dt.AsEnumerable() 
 
       .GroupBy(x => x.Field<string>(0), y => y.Field<string>(1)) 
 
       .ToDictionary(x => x.Key, y => y.FirstOrDefault()); 
 

 
     } 
 
    } 
 
    public class CSVReader 
 
    { 
 

 
     public DataSet ReadCSVFile(string fullPath, bool headerRow) 
 
     { 
 

 
      string path = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1); 
 
      string filename = fullPath.Substring(fullPath.LastIndexOf("\\") + 1); 
 
      DataSet ds = new DataSet(); 
 

 
      try 
 
      { 
 
       if (File.Exists(fullPath)) 
 
       { 
 
        string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + ";Extended Properties=\"Text;HDR={1};FMT=Delimited\\\"", path, headerRow ? "Yes" : "No"); 
 
        string SQL = string.Format("SELECT * FROM {0}", filename); 
 
        OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, ConStr); 
 
        adapter.Fill(ds, "TextFile"); 
 
        ds.Tables[0].TableName = "Table1"; 
 
       } 
 
       foreach (DataColumn col in ds.Tables["Table1"].Columns) 
 
       { 
 
        col.ColumnName = col.ColumnName.Replace(" ", "_"); 
 
       } 
 
      } 
 

 
      catch (Exception ex) 
 
      { 
 
       Console.WriteLine(ex.Message); 
 
       Console.ReadLine(); 
 
      } 
 
      return ds; 
 
     } 
 
    } 
 
} 
 
​