![](/img/trans.png)
[英]How to redirect a user back to where he was after login in asp.net mvc 3
[英]Redirect the user back to the source page from where it has come after succesfull login in asp.net
大家好,我正在为我的最后一个年度项目开发一个小的asp.net项目,我正在制作一个在线购物网站。 当用户单击详细信息按钮时,我在网站Login.aspx,detail.aspx和mobile.aspx中的mobile.aspx中有三个页面,当用户单击详细信息按钮时,它会使用response.redirect()将用户重定向到detail.aspx页面;步)。 现在在detail.aspx页面上,当用户单击addtoCart按钮时,该页面首先检查用户是否已登录(使用session [“ authenticated”]!= null方法)(如果已登录),则一切正常对我来说,问题出在当用户未登录并单击addtoCart按钮时,它将用户重定向到login.aspx页。在登录页面中,当用户单击登录按钮时,它将检查用户是否从详细信息页面重定向(使用session [ “ productID”]!= null),如果是,则应将用户重定向回detail.aspx页面,但这样做不行,这是我的代码,请帮助我
login.aspx
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
SqlConnection connection = new SqlConnection(conn);
connection.Open();
cmd = new SqlCommand("select COUNT(*) from customer_login where login_id = @a and pass_login=@b",connection);
cmd.Parameters.AddWithValue("@a", Login1.UserName);
cmd.Parameters.AddWithValue("@b", Login1.Password);
string user_name;
int i = Convert.ToInt32(cmd.ExecuteScalar().ToString());
ViewState["PreviousPage"] = Request.UrlReferrer.ToString();//to retrive the url from where it is redirected, this will be used to redirect the user from where it comes(detail.aspx)
if (i == 1)
{
e.Authenticated = true;
Session["authenticatedUser"] = Session.SessionID;
Session["loginid"] = Login1.UserName.ToString();
cmd = new SqlCommand("select f_name from customer where id = (select cust_id from customer_login where login_id = @a)", connection);
cmd.Parameters.AddWithValue("@a", Login1.UserName);
sdr = cmd.ExecuteReader();
sdr.Read();
user_name = sdr["f_name"].ToString();
sdr.Close();
if (Session["productID"] != null&&ViewState["PreviousPage"].ToString())//to check if the user is redirected from detail.aspx page
{
Session["user"] = user_name.ToString();
Response.Redirect(ViewState["PreviousPage"].ToString());
}
else
{
Session["user"] = user_name.ToString();
Response.Redirect("Index.aspx");
}
}
else
{
e.Authenticated = false;
}
}
登录的结果是它将用户重定向到index.aspx页面而不是在详细信息页面上
我在detail.aspx页面中找到了使用querystring发送源页面的URL(detail.aspx)的解决方案
Response.Redirect("~/login.aspx?redirect="+Request.Url.ToString());
然后将其检索到login.aspx中
if (Session["productID"] != null&&!string.IsNullOrEmpty(Request.QueryString["redirect"]))
{
Session["user"] = user_name.ToString();
Response.Redirect(Request.QueryString["redirect"].ToString()+"&ssid="+Session["authenticatedUser"].ToString());
//Response.Redirect(ViewState["PreviousPage"].ToString());
// Response.Redirect("~/shop/Cart.aspx?ssid="+Session["authenticatedUser"].ToString()+"&pr="+Session["productID"].ToString());
}
else
{
Session["user"] = user_name.ToString();
Response.Redirect("Default.aspx");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.