User desire page after login

Generally when we login it will redirect us to default page. But in case when we want to go to our desire page which we store in book mark. In that case, after login it will always redirect us to default page and not our desire page. So this article I will explain how to go to desire page instead of default page using Non-form authentication and Form authentication.

Download source code for Non-Form Authentication here

Download source for Form Authentication here.

1) Non-Form Authentication :-

For this I will create three pages, namely Login.aspx, Default.aspx and MyProfile.aspx.  I guess, you might come to know from page names what it will do. To go user desire page, I have created an session variable name Session["DesirePage"], which will store an your current URL address. And in login page, I will check if this session is null or not. if not, Then it will redirect user to its desire page.

Login.aspx

My Login page looks like below one :- 

 Login

 

Login page button event looks like as below :-

protected void btnSumbit_Click(object sender, EventArgs e)

{

  if (txtUserName.Text == "manojk" && txtPassword.Text == "manojk")

  {

 

      Session["UserName"] = txtUserName.Text;

 

      if (Session["DesirePage"] != null)

      {

          Response.Redirect(Convert.ToString(Session["DesirePage"]));

      }

      else

      {

          Response.Redirect("Default.aspx"); ;

      }

   }

}

 

As mention in code, I will check session variable Session[“DesirePage”] if its null or not. after check it will go to necessary condition.

Note :- I am not doing database connect to check for user id and password, as main thing I want to explain how to go user desire page.

 

Default.aspx

In this page, I am not doing anything special just checking if session[“UserName”] is null or not.

protected void Page_Load(object sender, EventArgs e)

{

  if (Session["UserName"] != null)

 {

   Response.Write(string.Format("welcome {0}, this is default page",Session["UserName"]));

 }

 else

 {

   Response.Redirect("Login.aspx");

 }

}

 

Note, that here Session["DesirePage"] check its not required as its an default page.

 

MyProfile.aspx

protected void Page_Load(object sender, EventArgs e)

{

  if (Session["UserName"] != null)

  {

     Response.Write(string.Format("welcome {0}, this is your profile page", Session["UserName"]));

  }

  else

  {

     Session["DesirePage"] = Request.RawUrl;

     Response.Redirect("Login.aspx");

   }

}

 

As you might notice in above code that I am assigning session[“DesirePage”] as Request.RawUrl. So, when you try to access MyProfile.aspx, and session[“UserName”] is null it will assign session[“DesirePage”] and then it will redirect to login page.

You can test this scenario by making MyProfile.aspx page as start up page.

 

2) Using Form Authentication :-

Using form authentication, I have to check one more step..  In form authentication, initially I have to check if Request.QueryString["ReturnUrl"] is null or not.

 

protected void btnSumbit_Click(object sender, EventArgs e)

{

 

 if (txtUserName.Text == "manojk" && txtPassword.Text == "manojk")

 {

 

    Session["UserName"] = txtUserName.Text;

 

    HttpCookie AuthCookie;

    AuthCookie = FormsAuthentication.GetAuthCookie(txtUserName.Text, true);

    AuthCookie.Expires = DateTime.Now.AddDays(10);

    Response.Cookies.Add(AuthCookie);

 

    if(Request.QueryString["ReturnUrl"] != null)

         Response.Redirect(Request.QueryString["ReturnUrl"]);

    else

    {

       if (Session["DesirePage"] != null)

         Response.Redirect(Convert.ToString(Session["DesirePage"]));

       else

          Response.Redirect("Default.aspx"); ;

 

     }

  }

}

 

I hope you like this article, how to go directly to desire page once login. Download source code has been given on top.

No Comments