[英]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.