LogIn Mechanism Using 3-tier

Posted by Sandhyab under ASP.NET category on | Points: 40 | Views : 2473
Use this code where you want to display like Welcome,"UserName":
<asp:LoginView runat="server" ID="LoginView1">
Welcome Guest,
<asp:LoginStatus runat="server" ID="LoginStatus1" />
<asp:LoginName runat="server" ID="LoginName1" />
<asp:LoginStatus runat="server" ID="LoginStatus1" />

And, LogIn using 3-tier:
UI Layer:

<asp:Login ID="Login1" runat="server" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderPadding="4"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="1em"
ForeColor="#333333" OnAuthenticate="Login1_Authenticate">
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
<TextBoxStyle Font-Size="0.8em" />
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />

Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;
using DotNetFundaBAL;

public partial class LogIn : System.Web.UI.Page
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
protected void Page_Load(object sender, EventArgs e)

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
bool isAuthenticated = false;
ArticleBAL b = new ArticleBAL();
string UserName = Login1.UserName;
string Password = Login1.Password;
object obj = b.LogIn(UserName, Password);
if (obj != null)
isAuthenticated = true;

if (isAuthenticated)
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);


In DAL Class write This Method:
 public object LogIn(string UserName, string Password)
using (SqlConnection conn = new SqlConnection(_constr))
string sql = "SELECT CustomerId from Registration WHERE UserName = @UserName AND Password = @Password";
using (SqlCommand cmd = new SqlCommand(sql, conn))
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Parameters.AddWithValue("@Password", Password);
object obj = cmd.ExecuteScalar();
return obj;


In BAL add this method:
public object LogIn(string UserName, string Password)
ArticleDAL i = new ArticleDAL();
return i.LogIn(UserName, Password);

<authentication mode="Forms">
<forms defaultUrl="~/Forums.aspx" loginUrl="~/LogIn.aspx" slidingExpiration="true" timeout="20"/>

Thanks & regards

Comments or Responses

Posted by: Anishabathija on: 12/12/2012 Level:Starter | Status: [Member] | Points: 10
Hi ,

good code.
But suppose you have two levels of entry like one Admin and the other user and you direct them to different pages !!
How do you check their credentials in the BC layer.

Login to post response