简体   繁体   中英

Redirect the user back to the source page from where it has come after succesfull login in asp.net

Hello guys I am working on a small asp.net project for my final year project, I am making a online shopping site. I have three pages in my site Login.aspx, detail.aspx and mobile.aspx in mobile.aspx when a user click on detail button it redirect the user to detail.aspx page using response.redirect();(no problem in this step). now in detail.aspx page when a user click on the addtoCart button the page first check if the user is logged in or not(using session["authenticated"]!=null method) if it is logged in then every thing goes ok according to me now the problem comes when user is not logged in and click on addtoCart button it redirect the user to login.aspx page In login page when a user click on login button it checks if user is redirected from the detail page(using session["productID"]!=null) if yes it should redirect the user back to the detail.aspx page but it not doing so here is my code please help me

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;
    }
}

the result of the login is it redirect the user to index.aspx page not on to the detail page

I found the solution I used querystring in the detail.aspx page to send the url of the source page(detail.aspx)

            Response.Redirect("~/login.aspx?redirect="+Request.Url.ToString());

and then retrieve it into the 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");
        }

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