-1

I'm new on ASP.NET. i am developing one jobportal website using asp.net C#. In website user after find their job post when try to click on "Apply Now" , if session empty, page will redirect to "CandidateLogin.aspx". After login the page will redirect to last visited page, and then user can apply. If user open the website and open login page, or directly enter login page URL, after login it will redirect to Candidate Profile page. Any help would be appreciated.

protected void btnLogin_Click(object sender, EventArgs e)
    {
        string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["jobforu"].ToString();
        SqlConnection conn = new SqlConnection(ConnString);
        SqlCommand cmd = new SqlCommand();
        conn.Open();
        cmd.Connection = conn;
        cmd.Parameters.Clear();
        SqlDataReader dr;

        canname = "";
        canpwd = "";
        canemail = "";
        session_canFirstName="";

        cmd.CommandText = "select  *  from  jobforu.dbo.can_master where can_email = @can_email and can_pwd = @can_pwd";
        cmd.Parameters.AddWithValue("@can_email", txtEmail.Text.Trim());
        cmd.Parameters.AddWithValue("@can_pwd", txtPassword.Text.Trim());
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            canname = dr["can_fname"].ToString() +" "+ dr["can_mname"].ToString() +" "+ dr["can_lname"].ToString();
            canpwd = dr["can_pwd"].ToString();
            canemail = dr["can_email"].ToString();

            session_canFirstName = dr["can_fname"].ToString();
            sessionCanCode = dr["can_code"].ToString();


        }
        dr.Close();
        conn.Close();
        cmd.Parameters.Clear();


        if (canname.Length > 1)
        {
            Session["canname"] = canname;
            Session["canpwd"] = canpwd;
            Session["canemail"] = canemail;
            Session["sessionCanCode"] = sessionCanCode;

            Response.Redirect(ViewState["GoBackTo"].ToString());

        }

        else
        {
            //Response.Write("Invalid user name or password");
            lblErrorMsg.Visible = true;
            lblErrorMsg.Text = "Invalid user name or password!";
            txtEmail.Text = "";
            txtPassword.Text = "";
            txtEmail.Focus();
        }


    }

Page Load:

protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["sessionCanCode"] != null)
        {
            Response.Redirect("jobseekers/Default.aspx?ref=m_icon");
        }

        if (!Page.IsPostBack)
        {
            ViewState["GoBackTo"] = Request.UrlReferrer;
            FillCapctha();
        }
        lblErrorMsg.Visible = false;

    }
  • Sadly, unlike _Professor Xavier_, most people are not psychic and can't see your code. Consider posting a code sample. Thanks and good luck! –  Feb 18 '15 at 05:31
  • possible duplicate of [How do I redirect to the previous action in ASP.NET MVC?](http://stackoverflow.com/questions/815229/how-do-i-redirect-to-the-previous-action-in-asp-net-mvc) – Rosdi Kasim Feb 18 '15 at 05:41
  • Mr Mickey Duncan I have update my question with code. – Bikash Mahata Feb 18 '15 at 05:44

1 Answers1

0

I am assuming you are using webforms. An you want to redirect after login to it's previous page. For that you can use the Request.UrlReferrerproperty. However this property will be null if the HTTP header value is not a URI or if the field was not set by the client UA.
You must never depend on this mechanism because it is set by the client, and you must never trust the client ( http://en.wikipedia.org/wiki/Defensive_programming ). And as stated, not all visitors will have the Referer header set.

Instead what you can do is use the Request.QueryString to pass the previous page URL to the LoginPage. ANd when user log's in use that URl to redirect back to the page.

Mahesh
  • 8,694
  • 2
  • 32
  • 53