apoorva003 says:
Dear!!!
I have two pages:
Login.aspx(two text box and one button control, it is working properly)
Welcome.aspx
I want that without login validation the welcome page must not show in browser. How can I do this?
After user Login from the Login page you can set session value and on welcome page just check the session is true or false or null. if its null or false then redirect to error or LoginPage. You can set session as false on logout button too.
HTML LoginPage.aspx
<div>
UseName:
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br />
<br />
Password:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="ValidateLogin" />
</div>
LoginPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Set IsUserLogin Session as False on Page laod
Session["IsUserLogin"] = false;
}
}
protected void ValidateLogin(object sender, EventArgs e)
{
if (this.ValidateLogin())
{
Session["IsUserLogin"] = true;
Response.Redirect("~/Welcome.aspx");
}
}
private bool ValidateLogin()
{
bool isUserExists = false;
string constring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("ValidateUser");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Username", txtUserName.Text);
cmd.Parameters.Add("@Password", txtPassword.Text);
cmd.Connection = conn;
conn.Open();
isUserExists = Convert.ToBoolean(cmd.ExecuteScalar());
conn.Close();
return isUserExists;
}
Welcome.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.IsUserLogedIn();
}
}
private void IsUserLogedIn()
{
if (Session["IsUserLogin"] == null || !Convert.ToBoolean(Session["IsUserLogin"]))
{
Response.Redirect("~/LoginPage.aspx");
}
}
If you are using Login control then refer below link if not using Form authentication Login control then ignore this