2015-03-19 28 views
0

我試圖在基於n層體系結構的ASP.NET C#中實現登錄功能。在多層體系結構中登錄ASP.NET

數據訪問:

public int userlogin(string user, string passw)//checking the user name and password 
{ 
    SqlConnection con = new SqlConnection(); 
    con.ConnectionString = GetConnectionString(); 
    con.Open(); 
    int id = 0; 
    string selectstr = "SELECT NurseName, password FROM Nurse2 WHERE NurseName = '" + user.Trim() + "' AND Password = '" + passw.Trim() + "'"; 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandText = selectstr; 
    cmd.CommandType = System.Data.CommandType.Text; 
    cmd.Connection = con; 
    SqlDataReader reader = cmd.ExecuteReader(); 
    while (reader.Read()) 
    { 
     id++; 
    } 
    cmd = null; 
    reader.Close(); 
    con.Close(); 
    return id; 
} 

表示層cs文件

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string name = TextBox1.Text; 
     string password = TextBox2.Text; 
    int id = da.userlogin(name, password); 
    if (id > 0) 
    { 
     Session["userName"] = name; 

     Response.Redirect("SubscribePage.aspx"); 

    } 
    else 
    { 
     Label1.Text = "invalid"; 
    } 

現在,我的問題是,當我按下按鈕,程序簡單地去else子句,即使我輸入正確的數據。在我看來,這一切似乎都沒有問題。

+0

錯誤...您是否以純文本格式存儲密碼?您似乎也正在修改**修改**用戶密碼! – cubrr 2015-03-19 08:49:16

+0

是的,我是。應該以不同的方式做? – 2015-03-19 08:53:38

+0

至於明文密碼,請看看這個:http://plaintextoffenders.com/faq/devs。至於破壞用戶密碼......你爲什麼要這麼做? – cubrr 2015-03-19 08:56:34

回答

-1

N層架構有助於分離您的代碼,因爲它是您的代碼跳過一層並且沒有充分利用業務邏輯層。這是一個有用的圖像;

Diagram representation

我還想添加一個額外的類來存儲用戶登錄信息,我猜你會與護士名一起更多的信息,存儲 - 您可以在這個類的實例存儲您的會話數據並在需要時丟棄;

public class User 
{ 
    public string Name  { get; set; } 
    /* Some other attributes - not your password though! */ 
} 

-

介紹;

protected void Button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      Session["User"] = BLL.userLogin(TextBox1.Text, TextBox2.Text); 
      Response.Redirect("SubscribePage.aspx"); /* If it reaches here, everything is okay */ 
     } 
     catch (Exception ex) 
     { 
      Label1.Text = ex.Message; 
     } 
    } 

業務層;

public static User userLogin(string username, string password) 
    { 
     User U = DAL.userLogin(username, password); 

     if (string.IsNullOrEmpty(U.Name)) 
      throw new Exception("Incorrect login details"); 

     return U; 
    } 

數據訪問層;

public static User userLogin(string username, string password) 
    { 
     using (SqlConnection con = new SqlConnection(GetConnectionString()) 
     { 
      User U = new User(); 

      SqlCommand cmd = new SqlCommand(@"SELECT NurseName, password 
               FROM Nurse2 
               WHERE NurseName = @user AND password = @pw", con); 

      cmd.Parameters.Add(new SqlParameter("@user", username)); 
      cmd.Parameters.Add(new SqlParameter("@pw", password)); 

      try 
      { 
       con.Open(); 
      } 
      catch (Exception ex) 
      { 
       throw new Exception("connetion problem", ex); 
      } 

      try 
      { 
       using (SqlDataReader rdr = cmd.ExecuteReader()) 
       { 
        while (rdr.Read()) 
        { 
         U = rdr["NurseName"]; 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       throw new Exception("problem with query", ex); 
      } 
      finally 
      { 
       con.Close(); /* Clean up regardless of the outcome */ 
       con.Dispose(); 
      } 

      return U; 
     } 
    } 

請仔細閱讀N層體系結構和try-catch語句。希望能幫助到你。我也會改進你的控制命名約定,使生活更輕鬆(即Label1 - > lblError)