Introduction

This article explains how to implement a login form using ASP.NET C# in a three-tier architecture. I have described it in details. You will also get many new and known concepts to learn in this article whether on the SQL Server Stored Procedure side or C# code side.

The following are the features of the Login Forms:

I have already explained how to create a registration form in ASP.NET C#. You can check this link.

Database design

Database design

Stored Procedure

  1. Create PROCEDURE [dbo].[usp_LoginVerification]
  2. @UserId nvarchar(50),
  3. @Password nvarchar(20),
  4. @ERROR VARCHAR(100) OUT
  5. AS
  6. Begin
  7. If exists (select 1 from MemberRegistration where UserId=@UserId and Password=@Password and IsLocked='0')
  8. Begin
  9. --declare
  10. update MemberRegistration set WrongLoginAttempt=0,IsLocked='0' Where UserId=@UserId
  11. set @ERROR=1
  12. select UserId,FirstName,MiddleName,LastName from MemberRegistration Where UserId=@UserId
  13. End
  14. Else
  15. Begin
  16. declare @LoginAttempt int;
  17. SET @LoginAttempt= (select WrongLoginAttempt from MemberRegistration where UserId=@UserId )
  18. update MemberRegistration set WrongLoginAttempt=@LoginAttempt+1 Where UserId=@UserId
  19. Set @ERROR='Your have entered wrong password'
  20. if @LoginAttempt>=4
  21. Begin
  22. update MemberRegistration set IsLocked='1' Where UserId=@UserId
  23. set @ERROR='Your Password is locked'
  24. End
  25. End
  26. Select @ERROR
  27. End

The following is the step-by-step procedure for development of the feature-enriched login form in a 3-tier architecture.

Step 1

Design your layered solution as below.

layered

Step 2

Open BELogin.cs and modify the code as below.

  1. namespace ABMS.BE
  2. {
  3. public class BELogin
  4. {
  5. private string userId;
  6. private string password;
  7. private string firstName;
  8. public string FirstName
  9. {
  10. get { return firstName; }
  11. set { firstName = value; }
  12. }
  13. private string middleName;
  14. public string MiddleName
  15. {
  16. get { return middleName; }
  17. set { middleName = value; }
  18. }
  19. private string lastName;
  20. public string LastName
  21. {
  22. get { return lastName; }
  23. set { lastName = value; }
  24. }
  25. public string Password
  26. {
  27. get { return password; }
  28. set { password = value; }
  29. }
  30. public string UserId
  31. {
  32. get { return userId; }
  33. set { userId = value; }
  34. }
  35. }
  36. }
Step 3

Modify the BL layer BLLogin.cs as below:
  1. using System.Data;
  2. namespace ABMS.BL
  3. {
  4. public class BLLogin
  5. {
  6. ABMS.DL.DLLogin objdal = new ABMS.DL.DLLogin();
  7. BE.BELogin objbeLogin = new BE.BELogin();
  8. public DataSet UserLogin(BE.BELogin objbeLogin)
  9. {
  10. try
  11. {
  12. return objdal.LoginCredential(objbeLogin);
  13. }
  14. catch
  15. {
  16. throw;
  17. }
  18. }
  19. }
  20. }
Step 4
Modify the DL Layer DLLogin.cs code as below:
  1. using System.Data.SqlClient;
  2. using System.Data;
  3. namespace ABMS.DL
  4. {
  5. public class DLLogin
  6. {
  7. SqlDBHelper sql = new SqlDBHelper();
  8. public DataSet LoginCredential(BE.BELogin belogin)
  9. {
  10. string connectionString = sql.ConnectionString();
  11. SqlConnection con = new SqlConnection();
  12. con.ConnectionString = connectionString;
  13. con.Open();
  14. SqlCommand cmd = new SqlCommand("usp_LoginVerification", con);
  15. cmd.CommandType = CommandType.StoredProcedure;
  16. cmd.Parameters.AddWithValue("@UserId", belogin.UserId);
  17. cmd.Parameters.AddWithValue("@Password", belogin.Password);
  18. cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
  19. cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
  20. SqlDataAdapter da = new SqlDataAdapter();
  21. DataSet ds = new DataSet();
  22. da = new SqlDataAdapter(cmd);
  23. da.Fill(ds);
  24. con.Close();
  25. return ds;
  26. }
  27. }
  28. }
Step 5
Design the UI as below:

Design the UI

Step 6
Modify the code of the UI Login.aspx.cs as below:
  1. using System;
  2. using System.Data;
  3. namespace ABMS.UI.Account
  4. {
  5. public partial class Login : System.Web.UI.Page
  6. {
  7. ABMS.BL.BLLogin objBL = new ABMS.BL.BLLogin();
  8. protected void Page_Load(object sender, EventArgs e)
  9. {
  10. }
  11. protected void btnLogin_Click(object sender, EventArgs e)
  12. {
  13. ABMS.BE.BELogin obJBE = new BE.BELogin();
  14. obJBE.UserId = UserName.Text.Trim();
  15. obJBE.Password = Password.Text.Trim();
  16. DataSet ds = new DataSet();
  17. ds = objBL.UserLogin(obJBE);
  18. if (ds.Tables.Count == 1)
  19. {
  20. lblErrorMsg.Text = ds.Tables[0].Rows[0][0].ToString();
  21. }
  22. else if (ds.Tables[1].Rows[0][0].ToString() == "1")
  23. {
  24. lblErrorMsg.Text = string.Empty;
  25. Session["UserId"] = ds.Tables[0].Rows[0][0].ToString();
  26. String name=null;
  27. if (!string.IsNullOrEmpty( ds.Tables[0].Rows[0][1].ToString()))
  28. {
  29. name = name + ds.Tables[0].Rows[0][1].ToString();
  30. }
  31. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0][2].ToString()))
  32. {
  33. name = name +" "+ ds.Tables[0].Rows[0][2].ToString();
  34. }
  35. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0][3].ToString()))
  36. {
  37. name = name + " "+ds.Tables[0].Rows[0][3].ToString();
  38. }
  39. Session["Name"] = name;
  40. Response.Redirect("~/Default.aspx");
  41. }
  42. }
  43. }
  44. }
Step 7
Run the application and enter the wrong password for multiple times. You will prompt the message as below:

message

Step 8
After attempting 4 times your password will be locked.

Password locked

Conclusion

In this article I have explained how to develop a feature-enriched Login form in a 3-tier architecture in ASP.NET C#.