2011-11-28 67 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