Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Login : Form
- {
- private readonly IAuthenticationRepository _authenticationRepository;
- private int _failedLoginCounter = 0;
- public Login()
- {
- InitializeComponent();
- // Initialize our repository
- _authenticationRepository = new AuthenticationRepository();
- }
- private void btnLogin_Click(object sender, EventArgs e)
- {
- var username = txtUsername.Text;
- var password = txtPassword.Text;
- if (string.IsNullOrEmpty(username))
- {
- MessageBox.Show("Please type your Username");
- txtUsername.Focus();
- return;
- }
- if (string.IsNullOrEmpty(password))
- {
- MessageBox.Show("Please type your Password");
- txtPassword.Focus();
- return;
- }
- // 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)
- CheckLogin(username, password);
- }
- private void CheckLogin(string username, string password)
- {
- try
- {
- string errorMessage;
- // We seperated our database code in a different class
- // We can now easily call the Login method from anywhere...
- // This piece of code now is reusable.
- var isAuthenticated = _authenticationRepository.Login(username, password, out errorMessage);
- if (isAuthenticated)
- {
- MessageBox.Show("Login Successful");
- Hide();
- }
- else
- {
- MessageBox.Show(errorMessage);
- _failedLoginCounter++;
- }
- // If the user has 3 failed login attempts on a row => close.
- if (_failedLoginCounter >= 3)
- Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- public interface IAuthenticationRepository
- {
- /// <summary>
- /// Tries to login using the specified Username and Password
- /// </summary>
- /// <param name="username">The username of the user who wants to login</param>
- /// <param name="password">The password of the user who wants to login</param>
- /// <param name="errorMessage">The errormessage will contain information when authentication has failed</param>
- /// <returns>Return true if authentication was succesful, false if there where any problems.</returns>
- bool Login(string username, string password, out string errorMessage);
- }
- public class AuthenticationRepository : IAuthenticationRepository
- {
- private readonly string _connectionString;
- public AuthenticationRepository()
- : this(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\...\Auth_Credentials.accdb;")
- {
- }
- public AuthenticationRepository(string connectionString)
- {
- _connectionString = connectionString;
- }
- public bool Login(string username, string password, out string errorMessage)
- {
- // Give out parameter a default value
- errorMessage = string.Empty;
- var numberOrResults = 0;
- // You need to use a using statement since OleDbConnection implements IDisposable
- // more inf: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.110).aspx
- using (OleDbConnection conDataBase = new OleDbConnection(_connectionString))
- {
- // You need to use a using statement since OleDbCommand implements IDisposable
- // more info: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand(v=vs.110).aspx
- using (OleDbCommand cmdDataBase = conDataBase.CreateCommand())
- {
- cmdDataBase.CommandText =
- "SELECT * FROM Auth_Credentials WHERE Username=@username AND Password = @password";
- cmdDataBase.Parameters.AddRange(new OleDbParameter[]
- {
- new OleDbParameter("@username", username),
- new OleDbParameter("@password", password)
- });
- // Open database if not open
- if (conDataBase.State != ConnectionState.Open)
- conDataBase.Open();
- // You need to use a using statement since OleDbDataReader inherits DbDataReader which implements IDisposable
- // more info: http://msdn.microsoft.com/en-us/library/system.data.common.dbdatareader(v=vs.110).aspx
- using (OleDbDataReader myReader = cmdDataBase.ExecuteReader())
- {
- while (myReader != null && myReader.Read())
- {
- numberOrResults++;
- }
- }
- }
- }
- if (numberOrResults > 0)
- errorMessage = "Duplicate Username or Password";
- else if (numberOrResults == 0)
- errorMessage = "Username or Password do not match";
- return numberOrResults == 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement