How to create Login Page in ASP.Net and its operations?


In this article I have explained about very basic concept Login page in ASP.Net for .NET beginners. In this login page I have check whether user entered valid login details or not if a login details is valid then allow entering website otherwise not.

Description:
Login page is easily created from using ASP.Net. The Login page contains two input box for collect user id and password. We check that details with database user table.

How to assign session value after login?
If user login details is valid then we need to assign session value. We can access that session value in the any other page of the same website.


protected void Button1_Click(object sender, EventArgs e)
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from usr_detail where uname='" + TextBox1.Text + "' and pwd='" + TextBox2.Text + "'",sqlcon);
dt.Clear();
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Session["user"] = TextBox1.Text;
Response.Redirect("Default2.aspx");
}
else {
TextBox1.Text = "";
TextBox2.Text = "";
Label1.Text = "Invalid Login Details!";
}
}

How to remove session value after logut?
Important point is we remove all session values after user click log out button. I write that code in the Default2.aspx page.

protected void LinkButton1_Click(object sender, EventArgs e)
{
//After click Log out we need to delete all session values
Session.RemoveAll();
Response.Redirect("Default.aspx");
}

How to protect whether user directly access other page of same website?
We can protect user don't allow access the second page or other page directly with help of session value. We can check session value in the page load event of each page. If he enter second page url directly then session value is null so it redirect automatically to Default.aspx page.

if (!Page.IsPostBack)
{
//Check whether user directly access this page or come from Login page.
if (Session["user"] == null)
{
//user access directly so not permit to view this page content
Response.Redirect("Default.aspx");
}
else
{
Label1.Text = "Welcome " + Session["user"].ToString();
}
}

Source Code Detail:
Here with I have attached entire source code for login concept. Download it and try to work on ASP.Net Login Page operation.
Front End : ASP.NET
Code Behind : C#

Conclusion:
I hope this Article is help to .Net Beginners for understand ASP.Net Login operation.


Attachments

  • ASP.Net_Login_Concept (42820-15327-LoginConcept.rar)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: