[英]Go back to page that needed authentication after successful login
我有一个网站,该网站包含几个需要登录才能查看的页面( deals.aspx
和settings.aspx
),以及成功登录后登录当前重定向到的页面( my_account.aspx
)。 我如何设置它,以便如果登录页面是从deals.aspx
或settings.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
和settings.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Response.Redirect("~/login.aspx");
}
}
而且my_account.aspx.cs
页中目前没有任何内容。
重定向到登录页面时添加一个参数:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Response.Redirect("~/login.aspx?from=ORIGINGPAGE"); //Set parameter here
}
}
然后,在登录页面中检索此参数,您将知道必须返回哪里!
您可以使用Request.UrlReferrer获取页面加载中的上一页,并在成功登录后重定向到该页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.