1
votes

In loggin comtrol I have ridrect the user to same url after login.In comment control when unregistered or unlogin user tries to comment I show him message which redirects hin in login control.Now I need to redirect him again back to comment page after login but I am in big dillema.

The following are my codes:

login.ascx.cs

protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            txtPassword.Text = obj.Encrypt(txtPassword.Text.Trim(), true);
            MySqlCommand cmd = new MySqlCommand("Select id,username,password From registration where username=@UserName And password= @password ", con);
            cmd.Parameters.AddWithValue("@UserName", txtusername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            //MySqlDataAdapter da1 = new MySqlDataAdapter("select id,username from registration where username='" + txtusername.Text + "'", con);
            con.Open();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            con.Close();

            string message = null;


            if (dt.Rows.Count > 0)
            {
                String t1 = dt.Rows[0]["id"].ToString();
                String username = dt.Rows[0]["username"].ToString();

                Session["UserId"] = t1;
                Session["username"] = username;

                Uri MyUrl = Request.UrlReferrer;
                Console.WriteLine(MyUrl);
                if (MyUrl != null)
                    HttpContext.Current.Request.UrlReferrer.ToString();

                string userName = Session["username"].ToString();
                //pnllogin.Visible = false;
                 message = "Hello " + userName + ", Welcome to our website.";
                 //Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup1('" + message + "');", true);
            }



            else
            {
                 message = "Sorry You cannot login, please provide correct Email and password";

            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);





        }

comment.ascx.cs

protected void btncomment_Click(object sender, EventArgs e) { try { if (Session["userid"] != null) { main AddComment = new main(); string mandir_id = Request.QueryString["id"];

        sQLcONN.Open();
        string SQL2 = "insert into comment (user_id,Comment,with_temple) values ('" + Session["userid"] + "','" + txtComment.Text + "','" + mandir_id + "')";
        sQLcONN.Close();
        AddComment.saveData(SQL2);
        Bindcomment();



    }
    else
    {
        //ModalPopupExtender1.Show();
       // Response.Write("You Must Login Or Register To Comment");
       // lblcommentmsg.Text="You Must Login Or Register To Comment";

        string message = "You Must Login Or Register To Comment";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);
    }
}
catch (Exception ex)
{
    Console.WriteLine("{0} Exception caught.", ex);
}

}

1
Why don't you store the URL in Session before you redirect them to login?nphx
I didn't get what you are saying.Please explain me clearly thankyou.user3247426
In comment control when unregistered or unlogin user tries to comment I show him message which redirects hin in login control. If I understand this correctly, you let users access comments, but when trying to post, you redirect them to login, correct?nphx

1 Answers

2
votes

Before actual redirection of unauthenticated users to the login page, why not introduce something like Session["returnPath"] = Request.RawUrl; and then when users get authenticated:

if (!string.IsNullOrWhiteSpace(Session["returnPath"]))
    Response.Redirect(Session["returnPath"]);

Additionally, ASP.NET MVC uses a returnUrl URL parameter instead of a session valuable. So your login URL would look like: http://www.example.com/Login?returnUrl=/the-page-you-were-on.aspx. Seems to work well.