成功登录后返回需要身份验证的页面

时间:2015-08-19 22:17:07

标签: c# asp.net

我有一个网站,其中有几个页面需要登录才能查看(deals.aspxsettings.aspx)以及登录成功登录后当前重定向到的页面(my_account.aspx)。我如何设置它,以便在从deals.aspxsettings.aspx调用登录页面时,如果它对用户进行身份验证,则会返回到它调用的页面而不是my_account.aspx

login.aspx.cs

protected void login_Click(object sender, EventArgs e)
{
    string loginUser = user.Text;
    string loginPassword = password.Text;
    bool success = false;

    System.Diagnostics.Debug.WriteLine(loginUser);
    System.Diagnostics.Debug.WriteLine(loginPassword);

    MySqlConnection conn = Database.getConnection();
    try
    {
        conn.Open();
        MySqlCommand cmd = new MySqlCommand(queryUsers);
        cmd.Connection = conn;
        cmd.Parameters.AddWithValue("@email", loginUser);
        cmd.Parameters.AddWithValue("@password", loginPassword);
        cmd.Prepare();

        if (success)
        {
            Session["user"] = user.ToString();
            Response.Redirect("~/my_account.aspx");
        }

    }
    catch (MySqlException ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }
    catch (System.InvalidOperationException ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }
    finally
    {
        conn.Close();
    }
}

deals.aspx.cssettings.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["user"] == null)
    {
        Response.Redirect("~/login.aspx");
    }
}

目前my_account.aspx.cs页面中没有任何内容。

2 个答案:

答案 0 :(得分:0)

重定向到登录页面时添加参数:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["user"] == null)
    {
        Response.Redirect("~/login.aspx?from=ORIGINGPAGE"); //Set parameter here
    }
}

然后,在登录页面中检索此参数,您将知道必须返回的位置!

答案 1 :(得分:0)

您可以使用Request.UrlReferrer在页面加载中获取上一页,并在成功登录后重定向到该页面。