Last active
August 29, 2015 14:07
-
-
Save Nosfheratu/6fedaaeaead375ebdf8e to your computer and use it in GitHub Desktop.
Simple Login in Code Behind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel; | |
| using System.Data; | |
| using System.Drawing; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Windows.Forms; | |
| using PoS.Entities; | |
| using PoS.Services; | |
| namespace PoS | |
| { | |
| public partial class frmLogin : Form | |
| { | |
| public frmMain frmMain; | |
| public User User { get; set; } | |
| bool Logged { get; set; } | |
| UsersService _usersService; | |
| public frmLogin() | |
| { | |
| InitializeComponent(); | |
| _usersService = new UsersService(); | |
| } | |
| private void frmLogin_Load(object sender, EventArgs e) | |
| { | |
| LoadUsers(); | |
| } | |
| private void btnLogin_Click(object sender, EventArgs e) | |
| { | |
| if (!string.IsNullOrEmpty(txtPassword.Text)) | |
| { | |
| Logged = Login(); | |
| if (Logged) | |
| { | |
| MessageBox.Show("Login Success!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); | |
| this.DialogResult = DialogResult.OK; | |
| this.Close(); | |
| } | |
| else | |
| { | |
| MessageBox.Show("Incorrect password, type your correct password.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); | |
| txtPassword.Focus(); | |
| this.DialogResult = DialogResult.None; | |
| } | |
| } | |
| else | |
| { | |
| MessageBox.Show("Please type a password.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); | |
| txtPassword.Focus(); | |
| this.DialogResult = DialogResult.None; | |
| } | |
| } | |
| private void btnCancel_Click(object sender, EventArgs e) | |
| { | |
| this.DialogResult = DialogResult.No; | |
| } | |
| private void frmLogin_FormClosing(object sender, FormClosingEventArgs e) | |
| { | |
| if (!Logged) | |
| this.DialogResult = DialogResult.No; | |
| } | |
| private void LoadUsers() | |
| { | |
| cmbUsers.DataSource = _usersService.GetAll(); | |
| cmbUsers.DisplayMember = "Username"; | |
| cmbUsers.ValueMember = "Id"; | |
| } | |
| private bool Login() | |
| { | |
| this.User = _usersService.Find(Convert.ToInt32(cmbUsers.SelectedValue)); | |
| return _usersService.Login(Convert.ToInt32(cmbUsers.SelectedValue), txtPassword.Text); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Reflection; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using Simple.Data; | |
| using PoS.Entities; | |
| namespace PoS.Services | |
| { | |
| public class UsersService | |
| { | |
| dynamic db; | |
| public UsersService() | |
| { | |
| db = Database.OpenFile(GetDBPath()); | |
| } | |
| private string GetDBPath() | |
| { | |
| return @"C:\MyPath\PosDB.sqlite"; | |
| } | |
| public bool Login(int userId, string password) | |
| { | |
| User user = db.Users.FindById(userId); | |
| return user.ValidatePassword(password); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment