0

我有这些类,我想用它来登录,检查电子邮件和密码是否相同,然后它将重定向到相应的页面。

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);
            }

    }

如何从 List getAccounts 中检索电子邮件和密码,以便我可以检查是否(电子邮件 ==来自列表帐户的电子邮件&& 密码 ==来自列表帐户的密码)?

4

2 回答 2

0

您是否要在帐户列表中查找电子邮件并检查输入的密码是否匹配?如果是这样,从表面上看,您只需按照以下方式循环遍历每个:

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

您也可以返回 aDictionary<string, Account>以简化和加快搜索。

更新

因此,而不是以下行:

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

插入

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

This assumes the getAccounts variable is accessible to isPasswordValid. In your current code it would not be visible, so you might want to pass it in as a parameter.

于 2012-07-04T06:47:21.387 回答
0

Try LINQ/extension methods.

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");
}

Write following code in other_page.aspx to read session key-value.

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

PS: Do not store password in the List<T>. You may assign Account object reference to the Session.

e.g

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

and to retrieve the account value from session:

Account account=Session["account"] as Account;
if(account!=null)
 {
   //read the property value of Account
 }
于 2012-07-04T06:47:51.763 回答