2011-12-13 106 views
2

我正在使用會話來驗證用戶身份。我的項目中有2個網頁。一個是的表單,另一種是EntryForm.aspx而另一種是log.aspx在asp.net中使用會話進行用戶身份驗證c#

在log.aspx我做

protected void Button1_Click(object sender, EventArgs e) 
{ 
     user_login loginu = new user_login(); 
     String uid_db = loginu.login(this.DropDownList1, this.TextBox1, this.TextBox2, this.Label5); 
     if (uid_db == "invalid") 
     { 
      Label5.Visible = true; 
      Label5.Text = "Invalid Login"; 
     } 
     else 
     { 

      string uname = uid_db.Substring(0, uid_db.IndexOf(",")).Trim(); 
      string[] tokens = uid_db.Split(','); 
      string dbname = tokens[tokens.Length - 1]; 

      Session["login"] = uname; 
      Session["db"] = dbname; 
      Response.Redirect("EntryForm.aspx"); 
     } 
} 

user_login我是把存儲在數據庫中的密碼,並輸入的值匹配其類由用戶。如果它發現一個值,我將它重定向到EntryForm.aspx。其中我檢查會話變量如下

protected void Page_Load(object sender, EventArgs e) 
    {// CHEK SESSION VARIABLE AND LOAD dropdownlist1 WITH VALUES 
     if (!IsPostBack) 
     { 
      String DB = ""; 
      String AccountID = ""; 
      if (Session["login"] != null && Session["db"] != null) 
      { 
       AccountID = Session["login"].ToString(); 
       DB = Session["db"].ToString(); 

       Label9.Text = AccountID; 
      } 
      else 
      { 
       Response.Redirect("log.aspx"); 
      } 
      HiddenField1.Value = DB.ToString(); 
      DropDown a = new DropDown(); 
      a.filldropdown1(this.DropDownList1, DB); 
     } 
    } 

這就是我所做的驗證用戶。在服務器上我也做了以下配置:

enter image description here

我在做Global.asax沒有設置任何東西,也不是web.config。我看過很多論壇,其中Global.asaxweb.config已配置。

我想知道我需要做什麼在我的項目中,以便非常有效地工作。我遇到會話超時的問題。我在我的服務器上設置了20分鐘,但有時突然我退出了。

請幫我理解使用會話進行身份驗證。

回答

3

首先,您必須編輯web.config並設置會話超時屬性。

<configuration> 
    <system.web> 
    <sessionState timeout="200"></sessionState> 
    </system.web> 
</configuration> 

另一個問題是使用IsPostBack塊。

protected void Page_Load(object sender, EventArgs e) 
    { 
    if (Session["login"] != null && Session["db"] != null) 
     { 
     String DB = ""; 
     String AccountID = ""; 
     AccountID = Session["login"].ToString(); 
     DB = Session["db"].ToString(); 
     Label9.Text = AccountID; 
     HiddenField1.Value = DB.ToString(); 
     DropDown a = new DropDown(); 
     a.filldropdown1(this.DropDownList1, DB); 
     } 
    else 
    { 
     Response.Redirect("log.aspx"); 
     } 
    } 
+0

非常感謝您,我想知道我在做什麼錯,以及服務器上關於會話的配置是否正確。 – Ishan

+0

@Ishan - 在您的代碼片段中,IsPostBack塊內的語句將在第一次加載頁面時執行。但是,您可以在檢查會話屬性的if主體內檢查IsPostBack屬性。 – adatapost

相關問題