Advertisement
Guest User

Untitled

a guest
Mar 11th, 2014
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1.     public partial class Login : Form
  2.     {
  3.         private readonly IAuthenticationRepository _authenticationRepository;
  4.         private int _failedLoginCounter = 0;
  5.  
  6.         public Login()
  7.         {
  8.             InitializeComponent();
  9.  
  10.             // Initialize our repository
  11.             _authenticationRepository = new AuthenticationRepository();
  12.         }
  13.        
  14.         private void btnLogin_Click(object sender, EventArgs e)
  15.         {
  16.             var username = txtUsername.Text;
  17.             var password = txtPassword.Text;
  18.  
  19.             if (string.IsNullOrEmpty(username))
  20.             {
  21.                 MessageBox.Show("Please type your Username");
  22.                 txtUsername.Focus();
  23.                 return;
  24.             }
  25.  
  26.             if (string.IsNullOrEmpty(password))
  27.             {
  28.                 MessageBox.Show("Please type your Password");
  29.                 txtPassword.Focus();
  30.                 return;
  31.             }
  32.  
  33.             // Seperate the login check and make it lously coupled from the UI (= do not refer to the UI elements, instead pass the values to a method)
  34.             CheckLogin(username, password);
  35.         }
  36.  
  37.         private void CheckLogin(string username, string password)
  38.         {
  39.             try
  40.             {
  41.                 string errorMessage;
  42.  
  43.                 // We seperated our database code in a different class
  44.                 // We can now easily call the Login method from anywhere...
  45.                 // This piece of code now is reusable.
  46.                 var isAuthenticated = _authenticationRepository.Login(username, password, out errorMessage);
  47.  
  48.                 if (isAuthenticated)
  49.                 {
  50.                     MessageBox.Show("Login Successful");
  51.                     Hide();
  52.                 }
  53.                 else
  54.                 {
  55.                     MessageBox.Show(errorMessage);
  56.                     _failedLoginCounter++;
  57.                 }
  58.  
  59.  
  60.                 // If the user has 3 failed login attempts on a row => close.
  61.                 if (_failedLoginCounter >= 3)
  62.                     Close();
  63.             }
  64.             catch (Exception ex)
  65.             {
  66.                 MessageBox.Show(ex.Message);
  67.             }
  68.         }
  69.     }
  70.  
  71.  
  72.     public interface IAuthenticationRepository
  73.     {
  74.         /// <summary>
  75.         /// Tries to login using the specified Username and Password
  76.         /// </summary>
  77.         /// <param name="username">The username of the user who wants to login</param>
  78.         /// <param name="password">The password of the user who wants to login</param>
  79.         /// <param name="errorMessage">The errormessage will contain information when authentication has failed</param>
  80.         /// <returns>Return true if authentication was succesful, false if there where any problems.</returns>
  81.         bool Login(string username, string password, out string errorMessage);
  82.     }
  83.  
  84.     public class AuthenticationRepository : IAuthenticationRepository
  85.     {
  86.         private readonly string _connectionString;
  87.  
  88.         public AuthenticationRepository()
  89.             : this(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\...\Auth_Credentials.accdb;")
  90.         {
  91.         }
  92.  
  93.         public AuthenticationRepository(string connectionString)
  94.         {
  95.             _connectionString = connectionString;
  96.         }
  97.  
  98.         public bool Login(string username, string password, out string errorMessage)
  99.         {
  100.             // Give out parameter a default value
  101.             errorMessage = string.Empty;
  102.  
  103.             var numberOrResults = 0;
  104.  
  105.             // You need to use a using statement since OleDbConnection implements IDisposable
  106.             // more inf: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.110).aspx
  107.             using (OleDbConnection conDataBase = new OleDbConnection(_connectionString))
  108.             {
  109.                 // You need to use a using statement since OleDbCommand implements IDisposable
  110.                 // more info: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand(v=vs.110).aspx
  111.                 using (OleDbCommand cmdDataBase = conDataBase.CreateCommand())
  112.                 {
  113.                     cmdDataBase.CommandText =
  114.                         "SELECT * FROM Auth_Credentials WHERE Username=@username AND Password = @password";
  115.  
  116.                     cmdDataBase.Parameters.AddRange(new OleDbParameter[]
  117.                     {
  118.                         new OleDbParameter("@username", username),
  119.                         new OleDbParameter("@password", password)
  120.                     });
  121.  
  122.                     // Open database if not open
  123.                     if (conDataBase.State != ConnectionState.Open)
  124.                         conDataBase.Open();
  125.  
  126.                     // You need to use a using statement since OleDbDataReader inherits DbDataReader which implements IDisposable
  127.                     // more info: http://msdn.microsoft.com/en-us/library/system.data.common.dbdatareader(v=vs.110).aspx
  128.                     using (OleDbDataReader myReader = cmdDataBase.ExecuteReader())
  129.                     {
  130.                         while (myReader != null && myReader.Read())
  131.                         {
  132.                             numberOrResults++;
  133.                         }
  134.                     }
  135.                 }
  136.             }
  137.  
  138.             if (numberOrResults > 0)
  139.                 errorMessage = "Duplicate Username or Password";
  140.             else if (numberOrResults == 0)
  141.                 errorMessage = "Username or Password do not match";
  142.  
  143.             return numberOrResults == 1;
  144.         }
  145.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement