2012-07-04 41 views
0

我有這些類,我想用它來登錄,以檢查電子郵件和密碼是否相同,然後它將重定向到相應的頁面。如何從列表中獲取值<class>

public class Account 
    { 
    public Account(){} 
    public int accID { get; set; } 
    public string emailAddress { get; set; } 
    public string password { get; set; } 
    public string name { get; set; } 
    public string company { get; set; } 
    public string position { get; set; } 
    public string department { get; set; } 
    public string mobileNo { get; set; } 
    public string officeNo { get; set; } 
    } 
public static SADataReader DoSelectQuery(String sql) 
    { 
     SAConnection myConnection = new SAConnection(DB_STR); 
     //open the connection 
     myConnection.Open(); 
     //Create a command object. 
     SACommand myCommand = myConnection.CreateCommand(); 

     //Specify a query. 
     myCommand.CommandText = sql; 

     //Create a DataReader for the command 
     SADataReader reader = myCommand.ExecuteReader(); 

     return reader; 
    } 
public static List<Account> getAllAccountFromReader(SADataReader reader){ 
     List<Account> results = new List<Account>(); 

     while (reader.Read()) 
     { 
      int accID = reader.GetInt32(0); 
      string emailAddress = reader.GetString(1); 
      string password = reader.GetString(2); 
      string name = reader.GetString(3); 
      string company = reader.GetString(4); 
      string position = reader.GetString(5); 
      string department = reader.GetString(6); 
      string mobileNo = reader.GetString(7); 
      string officeNo = reader.GetString(8); 


      Account Accounts = new Account(); 
      Accounts.accID = accID; 
      Accounts.emailAddress = emailAddress; 
      Accounts.password = password; 
      Accounts.name = name; 
      Accounts.company = company; 
      Accounts.position = position; 
      Accounts.department = department; 
      Accounts.mobileNo = mobileNo; 
      Accounts.officeNo = officeNo; 
      results.Add(Accounts); 
     } 
     return results; 
    } 
public static List<Account> getAllAccounts() 
    { 
     //Specify a query. 
     string sql = "SELECT accountID,emailAddress,password,name,company,position,department,mobileNo,officeNo FROM account"; 

     SADataReader reader = DoSelectQuery(sql); 
     List<Account> results = getAllAccountFromReader(reader); 
     return results; 
    } 

.CS文件檢查領域

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     string email = tbEmail.Text; 
     string password = tbPW.Text; 
     List<Account> getAccounts = MinuteDB.getAllAccounts(); 

     // Session["getAllAccount"] = getAccounts; 

     if(email ==?? && password == ??) 
      { 

         //Session["name"] = name.ToString(); 
         //Session["ID"] = Convert.ToInt32(accID.ToString()); 
         Response.Redirect("HomePage.aspx"); 
      } 

      else if (email == "" && password == "") 
      { 
       ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Please enter Login and Password!');", true); 
      } 
      else 
      { 
       ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Wrong Login Or Password!');", true); 
      } 

    } 

如何從列表getAccounts檢索電子郵件地址和密碼,這樣我可以從列表中帳戶檢查,如果(電子郵件== 電子郵件 & &密碼== 來自列表賬戶的密碼)??

+5

請不要將密碼存放。 –

+0

是的,但它的一個小項目無論如何..我只是想獲得領域,並與文本框進行比較。 – melvg

+3

即使是一個小項目,也不要這樣做。至少哈希他們,而當你哈希他們,添加鹽。如果你不知道你需要使用哪種哈希算法,[閱讀此](http://stackoverflow.com/questions/10948994/secure-hashing-in-net)。 – Steven

回答

0

嘗試使用LINQ /擴展方法。

var account = MinuteDB.getAllAccounts() 
       .Where(p=> p.emailAddress==email && p.password==password) 
       .FirstOrDefault(); 


if(account!=null) 
{ 
    Session["id"]=account.accID; 
    Session["name"]=account.name; 
    Response.Redirect("~/other_page.aspx"); 
} 

other_page.aspx中輸入以下代碼來讀取會話密鑰值。

int id=0; 
string name=""; 
if(Session["id"]!=null) 
    id=int.Parse(Session["id"].ToString()); 
if(Session["name"]!=null) 
    name=Session["name"]; 

PS:不要存放在List<T>密碼。您可以將對象引用Account分配給會話。

e.g

if(account!=null) 
    { 
     Session["account"]=account; 
     Response.Redirect("~/other_page.aspx"); 
    } 

,並檢索會話account值:

Account account=Session["account"] as Account; 
if(account!=null) 
{ 
    //read the property value of Account 
} 
+0

感謝您的回覆,但我不能得到這個工作.. – melvg

+0

@melvg - 有*錯字*。查看更新的帖子。 – adatapost

+0

它工作!感謝英雄! – melvg

0

你想在帳戶列表中找到電子郵件並檢查輸入的密碼匹配嗎?如果是這樣,表面上看你只是通過各線沿線的循環:

private bool isPasswordValid(string email, string password) 
{ 
    foreach (Account account in Accounts) 
    { 
    if (account.emailAddress != email) 
     continue; 
    return (account.password == password); 
    } 
    return false; 
} 

你可以或者返回到Dictionary<string, Account>簡化和加快搜索。

更新

因此,而不是下面的一行:

if(email ==?? && password == ??) 

插入

if (isPasswordValid(email, password)) 
    // it is valid 
else 
    // it is not valid, redirect 

這是假設getAccounts變量是isPasswordValid訪問。在你當前的代碼中,它不可見,所以你可能想把它作爲參數傳入。

+0

我真的不知道如何解釋它,但呃..我只是想檢索列表已完成的所有屬性,例如:accID,電子郵件,密碼等,從列表,獲取電子郵件和密碼值,並將其與文本框進行比較。如果文本框的值與獲取的值相同,則會重定向。 – melvg

+0

感謝您的回覆,稍後我會嘗試您的代碼。 – melvg