c# - Redirect the user back to the source page from where it has come after succesfull login in asp.net -
hello guys working on small asp.net project final year project, making online shopping site. have 3 pages in site login.aspx, detail.aspx , mobile.aspx in mobile.aspx when user click on detail button redirect user detail.aspx page using response.redirect();(no problem in step). in detail.aspx page when user click on addtocart button page first check if user logged in or not(using session["authenticated"]!=null method) if logged in every thing goes ok according me problem comes when user not logged in , click on addtocart button redirect user login.aspx page in login page when user click on login button checks if user redirected detail page(using session["productid"]!=null) if yes should redirect user detail.aspx page not doing here code please me
login.aspx
protected void login1_authenticate(object sender, authenticateeventargs e) { sqlconnection connection = new sqlconnection(conn); connection.open(); cmd = new sqlcommand("select count(*) customer_login login_id = @a , pass_login=@b",connection); cmd.parameters.addwithvalue("@a", login1.username); cmd.parameters.addwithvalue("@b", login1.password); string user_name; int = convert.toint32(cmd.executescalar().tostring()); viewstate["previouspage"] = request.urlreferrer.tostring();//to retrive url redirected, used redirect user comes(detail.aspx) if (i == 1) { e.authenticated = true; session["authenticateduser"] = session.sessionid; session["loginid"] = login1.username.tostring(); cmd = new sqlcommand("select f_name customer id = (select cust_id customer_login 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 user redirected 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 login redirect user index.aspx page not on detail page
i found solution used querystring in detail.aspx page send url of source page(detail.aspx)
response.redirect("~/login.aspx?redirect="+request.url.tostring());
and retrieve 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"); }
Comments
Post a Comment