I have created the sample with two pages
I have set the Home Page as Startup page and on page load i am checking if user is loged In or not.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["UserName"] == null)
{
Response.Redirect("Login.aspx?url=http://localhost:26540/RedirectBackToPageAfterLogin/Home.aspx");
}
}
}
If he is not logedIn then user will be redirected to LoginPage with QueryString(Url of this currectPage).
LoginPage
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
User Name
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Text="Test" />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox ID="txtPassword" Text="abc" TextMode="Password" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button Text="Login" OnClick="Login" runat="server" />
</td>
</tr>
</table>
</div>
</form>
C#
protected void Login(object sender, EventArgs e)
{
if (this.txtUserName.Text.Trim() == "Test")
{
Session["UserName"] = this.txtUserName.Text.Trim();
if (!string.IsNullOrEmpty(Request.QueryString["url"]))
{
Response.Redirect("Home.aspx");
}
}
}