Redirect To Clicked Page After Login

In most Websites, you can see some pages open without logging in but some pages require the user to login to open it.

The pages which require a user to  login will not open --  the system flow is that it first sends you to the Login page, followed by the page of your choice and then it will open. To achieve this requires extra coding and the logic to achieve it.

Example

In my Website, I have four pages:

Page Name Login Required
Yes / No
Description
• About MySelf No Display My information and this page does not require login.
• My Friends Yes This page will display my friend list and friend contact details This page requires login before opening.
• Coaching No Display my subject and topic which I teach and this page does not require login.
• Login No Login page to logged in.

Question/ Requirement / Task

In this scenario, when the user directly clicks on MyFriends, the system will redirect you to the login page, because the system requires a logged in user. After successful login, the system should open MyFriends page automatically.

Logical Answer

To achieve this,

  1. Check if the user is logged in or not in FriendList.aspx page.
    1. if (string.IsNullOrEmpty(Convert.ToString(Session["userid"])))  
    2.         {  
    3.           Response.Redirect("login.aspx?url=" + Server.UrlEncode(Request.Url.AbsoluteUri));  
    4.         }  
    Server.UrlEncode(Request.Url.AbsoluteUri)
    (This is the line of code having information for which the page is requested.)

    Note: In the code, given above, we are redirecting the user to login.aspx page with the URL query string with the value of the clicked page.

  2. In Login.aspx, when the user successfully logs in, we have to check the URL and redirect to the clicked page.
    1. string ReturnUrl = Convert.ToString(Request.QueryString["url"]);  
    2.         if (!string.IsNullOrEmpty(ReturnUrl))  
    3.         {  
    4.             Response.Redirect(ReturnUrl);  
    5.         }  
    6.         else  
    7.         {  
    8.             Response.Redirect("aboutmyself.aspx?msgs=" + "SuccessLogin");  
    9.         }  

Note In the code, given above, we are checking for URL query string value, if there is a value, followed by redirecting to the specific page; otherwise, aboutmyself.aspx.

Now, we will implement the above step by step.

OutPut

By default AboutMySelf.aspx opens, as shown below:

OutPut

Now, the user clicks My Friend menu option, shown below:

OutPut

Now, the user is redirected to Login.aspx page.

You can see the image, given below:

OutPut

You will see the address bar or URL of the page, http://localhost:8813/login.aspx?url=http%3a%2f%2flocalhost%3a8813%2fFriendList.aspx

The URL embedded above with the query string named URL is having the path of FriendList.aspx.

Afterwards, enter the User Name and Password. I checked the values in the backend code.

code

You will see in the image, given above, where ReturnUrl value is : http://localhost:8813/FriendList.aspx.

Display FriendList.aspx

Display

Step by Step to achieve our task:

  • Create Project

    Create a new ASP.NET Empty Web Site project named RedirectToPage.

  • Add four Web Form named as following

    Web Form File Name Descripton
    FriendList.aspx To display all my friends' contacts details in the grid view.
    AboutMySelf.aspx To display my complete resume.
    Login.aspx To check the user and password.
    Coaching.aspx To display the subject and topic which I teach.

  • Update your webconfig file with your sql server settings
    1. <connectionStrings>  
    2.  <add name="fileuploadConnectionString" connectionString="Data Source=192.168.1.50\sa;Initial Catalog=mbktest;Persist Security Info=True;User ID=sa;Password=clserver" providerName="System.Data.SqlClient"/>  
    3. </connectionStrings>  
  • Code FriendList.aspx
    1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="FriendList.aspx.cs" Inherits="FriendList" %>  
    2.   
    3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
    4.     <style>  
    5.         table, th, td {  
    6.                           border: 1px solid black;  
    7.                         }  
    8.     </style>  
    9. </asp:Content>  
    10. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
    11.     <br />  
    12.     <br />  
    13.     <h3>  
    14.         My Friends List  
    15.     </h3>  
    16.     <br />  
    17.     <table style="border:double 2px; text-align:left;" >  
    18.         <tr>  
    19.             <td style="font-size:larger">  
    20.                 Friend Name  
    21.             </td>  
    22.             <td>  
    23.                 Phone Number  
    24.             </td>  
    25.             </tr>  
    26.         <tr>  
    27.             <td>  
    28.                 Ashish  
    29.             </td>  
    30.             <td>  
    31.                 98121abc  
    32.             </td>  
    33.             </tr>  
    34.         <tr>  
    35.             <td>  
    36.                 Suhana  
    37.             </td>  
    38.             <td>  
    39.                 98221ghi  
    40.             </td>  
    41.          </tr>  
    42.         <tr>  
    43.             <td>  
    44.                 Nirupamaji  
    45.             </td>  
    46.             <td>  
    47.                 98221xyz  
    48.             </td>  
    49.          </tr>  
    50.         </table>  
    51. </asp:Content>  
  • Code FriendList.aspx.cs
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7.   
    8. public partial class FriendList : System.Web.UI.Page  
    9. {  
    10.     protected void Page_Load(object sender, EventArgs e)  
    11.     {  
    12.   
    13.         if (string.IsNullOrEmpty(Convert.ToString(Session["userid"])))  
    14.         {  
    15.             Response.Redirect("login.aspx?url=" + Server.UrlEncode(Request.Url.AbsoluteUri));  
    16.         }  
    17.              
    18.     }  
    19. }  
  • Code Login.aspx
    1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>  
    2.   
    3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
    4. </asp:Content>  
    5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
    6.     <br />  
    7.     <br />  
    8.     <p>  
    9.         For Friendlist and To View Friend Contacts detail Login is required.  
    10.     </p>  
    11.     <table style="border:double 2px; text-align:left;" >  
    12.         <tr>  
    13.             <td>  
    14.                 User Name  
    15.             </td>  
    16.             <td>  
    17.                 <asp:TextBox ID="txtusername" runat="server" placeholder="Username" /><br />  
    18.             </td>  
    19.             </tr>  
    20.         <tr>  
    21.             <td>  
    22.                 Password  
    23.             </td>  
    24.             <td>  
    25.                 <asp:TextBox ID="txtPassword" runat="server" placeholder="Password" /><br />  
    26.             </td>  
    27.             </tr>  
    28.             <tr>  
    29.                 <td colspan="2" style="text-align:center">  
    30.                         <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click"/>  
    31.                 </td>  
    32.             </tr>  
    33.   
    34.         </table>  
    35.   
    36.     <br />  
    37.     <br />  
    38.     <p>User Name :  ram</p>  
    39.     <p>Password  :  ram</p>  
    40.     <br />  
    41.     <br />  
    42. </asp:Content>  
  • Code Login.aspx.cs
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7.   
    8. public partial class Login : System.Web.UI.Page  
    9. {  
    10.     protected void Page_Load(object sender, EventArgs e)  
    11.     {  
    12.   
    13.           
    14.     }  
    15.     protected void btnLogin_Click(object sender, EventArgs e)  
    16.     {  
    17.         if ((txtusername.Text == "ram") && (txtPassword.Text == "ram"))  
    18.         {  
    19.             string ReturnUrl = Convert.ToString(Request.QueryString["url"]);  
    20.             if (!string.IsNullOrEmpty(ReturnUrl))  
    21.             {  
    22.                 Session["userid"] = "ram";  
    23.                 Response.Redirect(ReturnUrl);  
    24.             }  
    25.             else  
    26.             {  
    27.                 Response.Redirect("aboutmyself.aspx?msgs=" + "SuccessLogin");  
    28.             }  
    29.   
    30.         }  
    31.         else  
    32.         {  
    33.             Response.Write("<script>alert('Check your userid and password');</script>");  
    34.         }  
    35.     }  
    36. }  
  • Code MasterPage.master
    1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>  
    2. <!DOCTYPE html>  
    3.   
    4. <html xmlns="http://www.w3.org/1999/xhtml">  
    5. <head id="Head1" runat="server">  
    6.     <title></title>  
    7.     <asp:ContentPlaceHolder ID="head" runat="server">  
    8.     </asp:ContentPlaceHolder>  
    9.   
    10.     <style>  
    11.   
    12. div.menu5  
    13. {  
    14.     /*width:500px;margin:0 auto;*//*Uncomment this line to make the menu center-aligned.*/  
    15.     text-align:center;  
    16.     background:#332E28;  
    17.     border:1px solid black;  
    18.     font-size:12px;  
    19.     padding:1px;  
    20. }  
    21.   
    22. div.menu5 a  
    23. {  
    24.     display: inline-block;  
    25.     padding: 0 20px;  
    26.     background:#3A332C;  
    27.     border:1px solid #5E544A;  
    28.     color:#C4B09C;  
    29.     text-decoration:none;  
    30.     font: bold 12px Arial;  
    31.     line-height: 27px;  
    32.     margin-right:1px;  
    33. }  
    34.   
    35. div.menu5 a:hover, div.menu5 a.current  
    36. {  
    37.     background:#484037;  
    38.     color:#E6D4C3;  
    39. }  
    40.     </style>  
    41. </head>  
    42. <body>  
    43.     <form id="form1" runat="server">  
    44.   
    45.             <div class="menu5">  
    46.                 <a href="AboutMySelf.aspx">About Myself </a>  
    47.                 <a href="FriendList.aspx">My Friends</a>  
    48.                 <a href="Coaching.aspx">Coaching</a>  
    49.                 <a href="Login.aspx">Login</a>  
    50.             </div>  
    51.             <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">  
    52.             </asp:ContentPlaceHolder>  
    53.     </form>  
    54. </body>  
    55. </html>  
  • Code AboutMySelf.aspx
    1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="AboutMySelf.aspx.cs" Inherits="AboutMySelf" %>  
    2.   
    3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
    4. </asp:Content>  
    5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
    6.     <h2>Welcome to Manoj Kalla's  favourite portal www.c-sharpcorner.com</h2>  
    7.     <br />  
    8.     <br />  
    9.     <h3>C-sharpcorner is greatest platform to share your ideas and knowledge. </h3>  
    10.     <br />  
    11.     <h3>Please, help others to learn and grow. </h3>  
    12.     <br />  
    13.     <h4>Thak you, Mahesh Sir, Pravin Sir and Dinesh sir for your supports and so many gifts.</h4>  
    14.   
    15. </asp:Content>  
  • Code Coaching.aspx
    1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Coaching.aspx.cs" Inherits="Coaching" %>  
    2.   
    3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
    4. </asp:Content>  
    5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
    6.     <br />  
    7.     <br />  
    8.     <h4>C-sharpcorner teach us lot of things and values.</h4>  
    9.     <br />  
    10.     <h4>Now, I am spreading those value to world.</h4>  
    11. </asp:Content>