简体   繁体   English

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

[英]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 ). 我有一个网站,该网站包含几个需要登录才能查看的页面( deals.aspxsettings.aspx ),以及成功登录后登录当前重定向到的页面( 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 ? 我如何设置它,以便如果登录页面是从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.cs and settings.aspx.cs deals.aspx.cssettings.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. 而且my_account.aspx.cs页中目前没有任何内容。

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获取页面加载中的上一页,并在成功登录后重定向到该页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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