简体   繁体   中英

Go back to page that needed authentication after successful login

I've got a web site that has a couple of pages that require a login to view ( deals.aspx and settings.aspx ) and the page that the login currently redirects to after a successful login ( my_account.aspx ). How can I set it so that if the login page was called from deals.aspx or settings.aspx that when it authenticates the user it goes back to the page it called from instead of 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.cs and settings.aspx.cs

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

and there isn't anything in the my_account.aspx.cs page right now.

Add a parameter when redirecting to the login page:

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

Then, retrieve this parameter in the login page and you will know where you have to come back!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM