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

Database Table
- CREATE TABLE [dbo].[UserLogin](
- [UserID] [INT] IDENTITY(1,1) NOT NULL,
- [Username] [VARCHAR](50) NOT NULL,
- [Password] [NVARCHAR](250) NOT NULL,
- [PasswordSalt] [VARCHAR](50) NULL,
- [IsActive] [BIT] NOT NULL,
- [AddedOn] [DATETIME] NOT NULL,
- CONSTRAINT [PK_UserLogin] PRIMARY KEY CLUSTERED
- (
- [UserID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- ALTER TABLE [dbo].[UserLogin] ADD CONSTRAINT [DF_UserLogin_IsActive] DEFAULT ((0)) FOR [IsActive]
- GO
- Stored Procedure:
- -- =============================================
- -- Author: <Bhuban Rana>
- -- Create date: <2018-12-24>
- -- Description: <This Sp checks valid user in userlogin table>
- -- =============================================
- ALTER PROCEDURE [dbo].[usp_CheckValidUser]
- @Username varchar(100),
- @Password nvarchar(250)
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- DECLARE @Message varchar(100),
- @Data nvarchar(250),
- @Success bit
- IF EXISTS(SELECT * FROM UserLogin WHERE Username=@Username AND [Password]=@Password)
- BEGIN
- SET @Success=1;
- SET @Message='Valid User';
- END
- ELSE
- BEGIN
- SET @Success=0;
- SET @Message='Incorrect Username or Password!';
- END
- SELECT @Success AS Success,@Message AS [Message]
- END
In Service Class,
- public ResponseModel Login(LoginModel model) {
- ResponseModel response = new ResponseModel();
- try {
- using(IDbConnection connection = DbConnection.DbConnect()) {
- DynamicParameters parameters = new DynamicParameters();
- parameters.Add("@Username", model.Username);
- parameters.Add("@Password", model.Password);
- response = SqlMapper.Query < ResponseModel > (connection, "[dbo].[usp_CheckValidUser]", parameters, commandType: CommandType.StoredProcedure).FirstOrDefault();
- }
- } catch (Exception ex) {
- response.Success = false;
- response.Message = ex.Message.ToString();
- }
- return response;
- }
In Controller,
- public class LoginController {
- private LoginService _loginService;
- public LoginController() {
- _loginService = new LoginService();
- }
- }
Events
- private void btnLogin_Click(object sender, EventArgs e) {
- progressBar.Minimum = 0;
- progressBar.Maximum = 100;
- progressBar.Value = 10;
- progressBar.Step = 10;
- lblRequiredUname.Visible = false;
- lblRequiredPwd.Visible = false;
- if (string.IsNullOrEmpty(txtUsername.Text) && string.IsNullOrEmpty(txtPassword.Text)) {
- lblRequiredUname.Visible = true;
- lblRequiredPwd.Visible = true;
- } else if (string.IsNullOrEmpty(txtUsername.Text.Trim())) {
- lblRequiredUname.Visible = true;
- } else if (string.IsNullOrEmpty(txtPassword.Text.Trim())) {
- lblRequiredPwd.Visible = true;
- } else {
- progressBar.Visible = true;
- if (!bgWorker.IsBusy) {
- btnLogin.Enabled = false;
- progressBar.Style = ProgressBarStyle.Marquee;
- bgWorker.RunWorkerAsync();
- }
- }
- }
- private void bgWorker_DoWork(object sender, DoWorkEventArgs e) {
- ResponseModel response = new ResponseModel();
- LoginModel model = new LoginModel();
- model.Username = txtUsername.Text.Trim();
- // model.Password = txtPassword.Text.Trim();
- string passwordSalt = string.Empty;
- RegistrationModel registrationModel = loginController.GetUserDetailsByUsername(model.Username);
- if (registrationModel != null) {
- passwordSalt = registrationModel.PasswordSalt;
- model.Password = StaticMethods.EncryptString(passwordSalt, txtPassword.Text.Trim());
- response = loginController.Login(model);
- if (response.Success) {
- this.Invoke(new MethodInvoker(delegate() {
- this.Hide();
- using(Dashboard dashboard = new Dashboard()) {
- dashboard.ShowDialog();
- }
- }));
- } else {
- this.Invoke(new MethodInvoker(delegate() {
- txtUsername.ReadOnly = false;
- txtPassword.ReadOnly = false;
- }));
- btnLogin.Enabled = false;
- MessageBox.Show(response.Message, "Fail!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- } else {
- MessageBox.Show("Please enter valid username..", "Invalid Username!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
- progressBar.Style = ProgressBarStyle.Blocks;
- btnLogin.Enabled = true;
- }
Conclusion
After login, if the login process is successful, then it redirects to the homepage. Else, it shows an "Invalid Login" error message.

Join the conversation! Your thoughts help the community grow.