Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to redirect a member after login to the previous page he was surfing...
I tried but not working...can someone get me an idea how to do this..

The code i tried...

C#
if (Request.Params["ReturnUrl"] != null)
{
    string abc = Request.Params["ReturnUrl"].ToString();
    FormsAuthentication.RedirectFromLoginPage(txtmail.Text.Trim(), false);

}
else
{
    FormsAuthentication.SetAuthCookie(txtmail.Text.Trim(), false);

    Response.Redirect("Default.aspx");
}
FormsAuthentication.RedirectFromLoginPage(txtmail.Text.Trim(), false);
Posted
Comments
vijay__p 31-May-13 7:09am    
You do not need to check "ReturnUrl" is null or not. FormsAuthentication.RedirectFromLoginPage will automatically determine whether returnUrl exists or not. If exists then it will redirect to that Url else DefaultUrl.
User overloaded method of it.
http://msdn.microsoft.com/en-us/library/1f5z1yty.aspx

Add below code to your configuration file.(Sample code to have idea how to implement such things)

XML
<system.web>
  <authentication mode="Forms">
    <forms loginUrl="logon.aspx" name=".ASPXFORMSAUTH">
    </forms>
  </authentication>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>


Implement something like:

C#
void Logon_Click(object sender, EventArgs e)
  {
    if ((UserEmail.Text == "xyz@domain.com") &&
            (UserPass.Text == "12345"))
      {
          FormsAuthentication.RedirectFromLoginPage
             (UserEmail.Text, Persist.Checked);
      }
      else
      {
          Msg.Text = "Invalid credentials. Please try again.";
      }
  }


When any unauthenticated user tries to open any page,she will be automatically redirected to logon page.Once she provides correct username and password,then she will be redirected to the page she was requesting previously.

Happy coding.. :laugh:
 
Share this answer
 
v3
i use this method when user session time out
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Session["user"] != null)
            {
                
            }
            else
            {
                Response.Redirect("login.aspx?p=" + Request.RawUrl);
            }
        }
    }

and this method after successful login

C#
protected void butUserLogin_Click(object sender, EventArgs e)
    {
            Session["user"] = txtUserName.Text;
            if (Request.QueryString["p"] != null)
            {
                Response.Redirect(Request.QueryString["p"].ToString());
            }
       

    }


may this help you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900