2011-11-28 165 views
0

我創建了一個簡單的登錄頁面asp.net這需要用戶名和密碼的文本框,並與我已經存儲在數據庫和一個比較只有重定向如果是一樣的存儲在數據庫中。實現asp.net登錄和註銷

但正在發生的事情是,當我登錄,然後按返回鍵,然後再向前擠瀏覽器的按鈕回去,它仍然將用戶重定向。

所以任何人都可以幫助解決這個問題,因爲一旦註銷,它應該只在輸入憑據後重定向,而不是通過按下瀏覽器的按鈕。

protected void LoginButton_Click(object sender, EventArgs e) 
    { 
     int results = 0; 
     if (txtUsername.Text != "" && txtPassword.Text != "") 
     { 
      results = Validate_Login(txtUsername.Text, txtPassword.Text); 
     } 
     else 
     { 
      lblMessage.Text = "Please make sure that the username and the password is Correct"; 
     } 
     if (results == 1) 
     { 
      if (txtUsername.Text.Equals("admin")) 
      { 
       Response.Redirect("~/ConfigurationPage.aspx"); 
      } 
      else 
      { 
       Response.Redirect(""); 
      } 
     } 
     else 
     { 
      lblMessage.Text = "Invalid Login"; 
      lblMessage.ForeColor = System.Drawing.Color.Red; 
     } 
    } 

    public int Validate_Login(String username, String password) 
    { 
     var conString = ConfigurationManager.ConnectionStrings["ConnectionString"]; 
     string strConnString = conString.ConnectionString; 
     int results = 0; 

     using (SqlCommand cmdselect = new SqlCommand 
            { 
             CommandType = CommandType.StoredProcedure, 
             CommandText = "[dbo].[prcLoginv]" 
            }) 
     { 

      cmdselect.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = username; 
      cmdselect.Parameters.Add("@UPassword", SqlDbType.VarChar, 50).Value = password; 
      cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4); 

      cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output; 



      try 
      { 
       if (connection == null) 
       { 
        connection = new SqlConnection(strConnString); 
        connection.Open(); 
        cmdselect.Connection = connection; 
        cmdselect.ExecuteNonQuery(); 
        results = (int) cmdselect.Parameters["@OutRes"].Value; 
       } 
       else if (connection.State == ConnectionState.Closed) 
       { 
        connection.Open(); 
        cmdselect.ExecuteNonQuery(); 
        results = (int) cmdselect.Parameters["@OutRes"].Value; 
       } 

      } 
      catch (SqlException ex) 
      { 
       lblMessage.Text = ex.Message; 
      } 
      finally 
      { 
       if (connection != null) 
       { 
        connection.Close(); 
       } 
      } 
     } 
     return results; 
    } 
+1

我們需要你的一些代碼來更好地幫助你。請修改您的帖子併發布您的相關代碼。 –

+0

所以我不確定你真的在檢查證書和/或使用會話對象。你需要發佈**相關的**代碼。 – gideon

+0

我已經添加了我的代碼..感謝您的建議:) –

回答

1

我曾經有過這種情況。實際上,當您按下後退和前進按鈕時,服務器中不會​​發生任何事情。所以我通常做的是還檢查客戶端(我爲您在客戶端的會話變量)上:

<script> 
    var isLogged = '<%=Session["Logged"] %>'; 
    if (isLogged != 'True') { 
     window.location.href = 'About.aspx'; 
    } 
</script> 

這只是一個簡單的例子。但請注意,如果用戶點擊後退或前進按鈕並不重要,只要發生回發,他們將從該頁面中刪除。

祝你好運!

1

假設您正在進行表單身份驗證,您只需致電FormsAuthenticatication.SignOut();在您的登錄頁面的* Page_Load *上。另外,我會打電話Session.Abandon();清除會話。類似這樣的:

Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostback) 
    { 
    FormsAuthenticatication.SignOut(); 
    Session.Abandon(); 
    } 
} 

這將解決您的問題。

參考:http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.signout.aspx