Simple Login Form In Desktop Application Using WinForm

In a desktop application, we also need a login form just like in a web application. I have developed a simple login page in the desktop using Windows Forms. 
 
Login Form Sample
 
Simple Login Form In Desktop Application Using WinForm
 
Database Table
  1. CREATE TABLE [dbo].[UserLogin](  
  2.    [UserID] [INT] IDENTITY(1,1) NOT NULL,  
  3.    [Username] [VARCHAR](50) NOT NULL,  
  4.    [Password] [NVARCHAR](250) NOT NULL,  
  5.    [PasswordSalt] [VARCHAR](50) NULL,  
  6.    [IsActive] [BITNOT NULL,  
  7.    [AddedOn] [DATETIME] NOT NULL,  
  8. CONSTRAINT [PK_UserLogin] PRIMARY KEY CLUSTERED  
  9. (  
  10.    [UserID] ASC  
  11.    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  12. ON [PRIMARY]  
  13. GO  
  14. ALTER TABLE [dbo].[UserLogin] ADD CONSTRAINT [DF_UserLogin_IsActive] DEFAULT ((0)) FOR [IsActive]  
  15. GO  
  16.    
  17. Stored Procedure:   
  18. -- =============================================  
  19. -- Author:  <Bhuban Rana>  
  20. -- Create date: <2018-12-24>  
  21. -- Description: <This Sp checks valid user in userlogin table>  
  22. -- =============================================  
  23. ALTER PROCEDURE [dbo].[usp_CheckValidUser]  
  24. @Username varchar(100),  
  25. @Password nvarchar(250)  
  26. AS  
  27. BEGIN  
  28. -- SET NOCOUNT ON added to prevent extra result sets from  
  29. -- interfering with SELECT statements.  
  30. SET NOCOUNT ON;  
  31. DECLARE @Message varchar(100),  
  32. @Data nvarchar(250),  
  33. @Success bit  
  34. IF EXISTS(SELECT * FROM UserLogin WHERE Username=@Username AND [Password]=@Password)  
  35. BEGIN  
  36. SET @Success=1;  
  37. SET @Message='Valid User';  
  38. END  
  39. ELSE  
  40. BEGIN  
  41. SET @Success=0;  
  42. SET @Message='Incorrect Username or Password!';  
  43. END  
  44. SELECT @Success AS Success,@Message AS [Message]  
  45. END  
In Service Class,
  1. public ResponseModel Login(LoginModel model) {  
  2.     ResponseModel response = new ResponseModel();  
  3.     try {  
  4.         using(IDbConnection connection = DbConnection.DbConnect()) {  
  5.             DynamicParameters parameters = new DynamicParameters();  
  6.             parameters.Add("@Username", model.Username);  
  7.             parameters.Add("@Password", model.Password);  
  8.             response = SqlMapper.Query < ResponseModel > (connection, "[dbo].[usp_CheckValidUser]", parameters, commandType: CommandType.StoredProcedure).FirstOrDefault();  
  9.         }  
  10.     } catch (Exception ex) {  
  11.         response.Success = false;  
  12.         response.Message = ex.Message.ToString();  
  13.     }  
  14.     return response;  
  15. }  
In Controller,
  1. public class LoginController {  
  2.     private LoginService _loginService;  
  3.     public LoginController() {  
  4.         _loginService = new LoginService();  
  5.     }  
  6. }  
Events
  1. private void btnLogin_Click(object sender, EventArgs e) {  
  2.     progressBar.Minimum = 0;  
  3.     progressBar.Maximum = 100;  
  4.     progressBar.Value = 10;  
  5.     progressBar.Step = 10;  
  6.     lblRequiredUname.Visible = false;  
  7.     lblRequiredPwd.Visible = false;  
  8.     if (string.IsNullOrEmpty(txtUsername.Text) && string.IsNullOrEmpty(txtPassword.Text)) {  
  9.         lblRequiredUname.Visible = true;  
  10.         lblRequiredPwd.Visible = true;  
  11.     } else if (string.IsNullOrEmpty(txtUsername.Text.Trim())) {  
  12.         lblRequiredUname.Visible = true;  
  13.     } else if (string.IsNullOrEmpty(txtPassword.Text.Trim())) {  
  14.         lblRequiredPwd.Visible = true;  
  15.     } else {  
  16.         progressBar.Visible = true;  
  17.         if (!bgWorker.IsBusy) {  
  18.             btnLogin.Enabled = false;  
  19.             progressBar.Style = ProgressBarStyle.Marquee;  
  20.             bgWorker.RunWorkerAsync();  
  21.         }  
  22.     }  
  23. }  
  24. private void bgWorker_DoWork(object sender, DoWorkEventArgs e) {  
  25.     ResponseModel response = new ResponseModel();  
  26.     LoginModel model = new LoginModel();  
  27.     model.Username = txtUsername.Text.Trim();  
  28.     // model.Password = txtPassword.Text.Trim();  
  29.     string passwordSalt = string.Empty;  
  30.     RegistrationModel registrationModel = loginController.GetUserDetailsByUsername(model.Username);  
  31.     if (registrationModel != null) {  
  32.         passwordSalt = registrationModel.PasswordSalt;  
  33.         model.Password = StaticMethods.EncryptString(passwordSalt, txtPassword.Text.Trim());  
  34.         response = loginController.Login(model);  
  35.         if (response.Success) {  
  36.             this.Invoke(new MethodInvoker(delegate() {  
  37.                 this.Hide();  
  38.                 using(Dashboard dashboard = new Dashboard()) {  
  39.                     dashboard.ShowDialog();  
  40.                 }  
  41.             }));  
  42.         } else {  
  43.             this.Invoke(new MethodInvoker(delegate() {  
  44.                 txtUsername.ReadOnly = false;  
  45.                 txtPassword.ReadOnly = false;  
  46.             }));  
  47.             btnLogin.Enabled = false;  
  48.             MessageBox.Show(response.Message, "Fail!", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  49.         }  
  50.     } else {  
  51.         MessageBox.Show("Please enter valid username..""Invalid Username!", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  52.     }  
  53. }  
  54. private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {  
  55.     progressBar.Style = ProgressBarStyle.Blocks;  
  56.     btnLogin.Enabled = true;  
  57. }  
Conclusion
 
After login, if the login process is successful, then it redirects to the homepage. Else, it shows an "Invalid Login" error message.