Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
How to create login page in asp.net using 3 tier architecture
53834 Views
Intoduction 
I have ceated a simple login page with username and password in 3 layer achitecture.I have done asp validation for that fields.
If the user name and password not matched. 
If the user name and password matched.User rediect to different page 
Conclusion 
I think my lesson helped you.
For learn about creating registration page and logout see my asp.net tutorials.
Preview of login page 

In this i have written some java script for user name and password.When you click the text box the name inside the text box(username) will disappear.
Preview : If the fields not entered  

If you click the login button without filling the fields.it shows message to enter the user name and password.
Sql Database table  

In this program the user name and password checking in this table, If the username and password matched the users redirect to respective pages according to the user type.
Solution Explorer 

I have created this login page in 3 tier architecture.
Inside BAL class library i have created class named balLogin.cs
Inside DAL class library i have created class named dalLogin.cs
Inside cdManage application i have created web form named Home.aspx
Below i gave code for all the classes and web form.
Home.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="cdManage.Home" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 436px;
            text-align: center;
            height: 45px;
        }
        .style2
        {
            color: #0066CC;
            text-align: center;
        }
        .style7
        {
            height: 45px;
            color: #0066CC;
            text-align: left;
        }
        .style8
        {
            height: 45px;
            color: #0066CC;
            text-align: center;
        }
        .style9
        {
            height: 45px;
            color: #0066CC;
            width: 256px;
            text-align: center;
        }
        .style10
        {
            width: 237px;
            text-align: center;
            height: 26px;
        }
        .style11
        {
            width: 225px;
            text-align: center;
            height: 26px;
        }
        .style12
        {
            color: #0066CC;
            width: 256px;
            text-align: center;
        }
        .style13
        {
            color: #0066CC;
            text-align: left;
        }
        .style14
        {
            width: 436px;
            text-align: center;
            height: 30px;
        }
        .style15
        {
            height: 30px;
            color: #0066CC;
            width: 256px;
            text-align: center;
        }
        .style16
        {
            height: 30px;
            color: #0066CC;
            text-align: left;
        }
        .style17
        {
            width: 436px;
            text-align: center;
            height: 38px;
        }
    </style>
</head>
<body>
    <form id="Form1" runat="server">
    <div class="page">
        <div class="header">
            <div class="loginDisplay">
            </div>
            <div class="clear hideSkiplink">
            </div>
        </div>
        <script type="text/javascript" >
            function do_highlight(ele) {
                document.getElementById(ele.id).style.color = "black";
                if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "Username") {
                    document.getElementById(ele.id).value = "";
                }
                if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "Password") {
                    document.getElementById(ele.id).value = "";
                }
            }

            function do_unfocus(ele) {
                if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "") {
                    document.getElementById(ele.id).value = "Username";
                }
                if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "") {
                    document.getElementById(ele.id).value = "Password";
                }

                if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "Username")
                    document.getElementById(ele.id).style.color = "rgb(70,70,70)";

                if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "Password")
                    document.getElementById(ele.id).style.color = "rgb(70,70,70)";
            }
        </script>
        
        <div class="clear">
            <table style="width:100%;">
                <tr>
                    <td style="color: #0033CC" class="style1" colspan="2">
                        <h3 class="style2"><strong> Members Login Here</strong></h3></td>
                </tr>
                <tr>
                    <td class="style14" colspan="2">
                        &nbsp;&nbsp;
                        <asp:TextBox ID="UserName"  runat="server" onfocus="do_highlight(this);"
                            onblur="do_unfocus(this);" value="Username"  
                            MaxLength="20" ForeColor="#707070"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:TextBox ID="PassLog" runat="server"  onfocus="do_highlight(this);"
                            onblur="do_unfocus(this);" value="Password" ForeColor="#707070"
                            MaxLength="10" TextMode="Password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style11">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                            ControlToValidate="UserName" ErrorMessage="Please Enter the User Name"
                            ForeColor="Red" InitialValue="Username" ValidationGroup="login_gr"></asp:RequiredFieldValidator>
                    </td>
                    <td class="style10">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                            ControlToValidate="PassLog" ErrorMessage="Please Enter the Password"
                            ForeColor="Red" InitialValue="Password" ValidationGroup="login_gr"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="style17" colspan="2">
                        <asp:Button ID="Login" class="button_example" runat="server" Text="Login"
                            onclick="Login_Click" ValidationGroup="login_gr" Height="38px" />
                    </td>
                </tr>
                <tr>
                    <td class="style1" colspan="2">
                        <asp:Label ID="log_mess" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>
Home.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using BAL;

namespace cdManage
{
    public partial class Home : System.Web.UI.Page
    {
        boRegister register = new boRegister();
        balRegister balregister = new balRegister();
        balLogin ballogin = new balLogin();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Login_Click(object sender, EventArgs e)
        {
            string userName = UserName.Text;
            string password = PassLog.Text;
            int a = ballogin.ballog(userName, password);
            UserName.Text = "";
            PassLog.Text = "";
            if (a == 1)
            {
                Session["userName"] = userName;
                string type = ballogin.usertype(userName, password);
                Session["usertype"] = type;
                Response.Redirect("userHome.aspx");
            }
            else if (a == 2)
            {
                Session["userName"] = userName;
                Response.Redirect("adminHome.aspx");
            }
            else
            {
                log_mess.Text = "Enter the valid id";
            }   
        }
    }
}
BalLogin.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BAL
{
    public class balLogin
    {
        dalLogin dallogin = new dalLogin();
        public int ballog(string userid, string pass)//checking the usename and password
        {
            try
            {
                int a = dallogin.userlogin(userid, pass);
                string b = dallogin.userlog(userid, pass);
                if (a == 1 && (b == "Level1" || b == "Level2" || b == "Level3" ))
                {
                    return 1;
                }
                else if (a == 1 && b == "admin")
                {
                    return 2;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ec)
            {
                ec.GetType();
            }
            return 0;
        }
        public string usertype(string user, string pass)//checking the userType
        {
            try
            {
               return dallogin.userlog(user, pass);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
DalLogin.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class dalLogin
    {
        SqlConnection con = new SqlConnection(@"data source=LAKSHANYA\SQLEXPRESS;database=master;integrated security=true");
        public int userlogin(string user, string passw)//checking the user name and password.If matched count 1 not 0.
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select count(*) from user_table where userName='" + user + "'and password='" + passw + "'", con);
                int a = Convert.ToInt32(cmd.ExecuteScalar());
                return a;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }
        public string userlog(string user, string passw)//getting the userType
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select userType from user_table where userName='" + user + "' and password='" + passw + "'", con);
                string a = cmd.ExecuteScalar().ToString();
                return a;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
Best Lessons of "Asp.net/Ado.net/C#.net(Dotnet)"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details