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

    }

リスト 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;
}

または、 a を返しDictionary<string, Account>て検索を簡素化し、高速化することもできます。

アップデート

したがって、次の行の代わりに:

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

入れる

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

これは、getAccounts 変数が isPasswordValid にアクセスできることを前提としています。現在のコードでは表示されないため、パラメーターとして渡すことをお勧めします。

于 2012-07-04T06:47:21.387 に答える
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オブジェクト参照をセッションに割り当てることができます。

例えば

 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
 }
于 2012-07-04T06:47:51.763 に答える