Creating Login Form With Authentication For User Login Using C#

INTRODUCTION

I have designed a login form in Visual Studio 2015 using Windows Form Application with C# code, wherein the username and password get validated at the entry level and would deny entry for users who have unauthenticated credentials. The objective is to be more precise for the user with his/her username and password created by the administrator and hence, could not be changed under any circumstance without prior permission from the administrator. Also, to overcome numerous attempts, the validation stops the user from not more than three attempts. If all three attempts are proven wrong, access is denied for the user and he/she has to communicate with the administrator for login. To make the login form more creative, I have added picture box which would display picture for login and whether access has been granted or not for that particular user. It can be altered with the user photo to look more attractive whenever he/she logs into the application.

Prerequisites
         
         1. Visual Studio 2015
         2. System configuration with Windows 8.1 or Windows 10
 
Steps to create a login page.
 
Step 1

Open Visual Studio 2015, navigate to File >> New, and choose Project to open a new project.

 
Step 2

After completing Step 1, a new window opens up with templates for which the application can be chosen. The login form is developed using Visual C# and selecting the corresponding Windows Forms Application. The selected work area could be saved in this stage by giving desired form name in Name text area. The form name and the solution name will be the same that the user types. After completing this, click OK.
 
 
Step 3
 
Windows Form creates a work space by displaying an empty form. The user can select Toolbox from left-top corner to bring the desired tools required for designing. I have brought the following tools inside my form - label (4 nos), textbox (2 nos), button (2 nos), and picturebox (1). Picturebox is included in login form to add creativity and it displays the corresponding pictures for users who will be logging into the system.
 
 
Step 4
 
Next stage is for converting the tools brought inside the form into meaningful interactives. Tools are named according to their usage by right clicking on any tool and then selecting Properties. This displays a window on right side of the screen. Select Text option and type the tool name. Label 4 is left empty to display the validating message which is inside the coding.

 

Step 5

In the Solution Explorer, select Form1.cs which is the work space for our creation. Start to code inside the Main method. This is my code in which I have included how the tools will be reacting when the project is run. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace login_form  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         static int attempt = 3;    
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.           
  22.         private void button2_Click(object sender, EventArgs e)  
  23.         {  
  24.             this.Close();  
  25.         }  
  26.   
  27.         private void button1_Click(object sender, EventArgs e)  
  28.         {  
  29.             string username = textBox1.Text;  
  30.             string password = textBox2.Text;  
  31.   
  32.                 if ((this.textBox1.Text == "Admin") && (this.textBox2.Text == "admin"))  
  33.             {  
  34.                 attempt = 0;  
  35.                 pictureBox1.Image = new Bitmap(@"C:\Users\Mic 18\Desktop\granted.jpg");  
  36.                 MessageBox.Show("you are granted with access");  
  37.                            
  38.   
  39.             }  
  40.             else if ((attempt == 3) && (attempt > 0))  
  41.             {  
  42.                 pictureBox1.Image = new Bitmap(@"C:\Users\Mic 18\Desktop\images1.jpg");  
  43.                 label4.Text = ("You Have Only " + Convert.ToString(attempt) + " Attempt Left To Try");  
  44.                 --attempt;  
  45.             }  
  46.                else  
  47.             {  
  48.                 pictureBox1.Image = new Bitmap(@"C:\Users\Mic 18\Desktop\denied.jpg");  
  49.                 MessageBox.Show("you are not granted with access");  
  50.             }  
  51.           
  52.           }  
  53.     }  
  54. }  
 

Step 6

The completed form looks like this and it is now running. To start the application, press F5. The user who uses this form will type the username and desired password to enter into the next level.
 
 
 
Step 7

By typing the correct username and password and clicking the LOGIN button, the picture box changes the default picture with a different picture and a message box is prompted telling about the code validation so that the user can start accessing the application further. 
 
 
Step 8

If the user types wrong username or password, the label under picture box displays a message with the number of attempts left out for the user to try.
 
 

Step 9

I have limited the user entry with only three attempts. The code checks for the correct entry in username and password. If all the three attempts contain either wrong username or wrong password, the user will not be allowed to continue his/her process with the application. This is shown in the below picture.

 

Summary

In the above steps, I have explained how to create a simple login form using C# in Windows Form Application and validating the username and password with limited attempts as well as changing the pictures according to the type of user. I have explained a scenario with only one user login. Future enhancements will be including a database with usernames and passwords where we can add any number of users and respective passwords.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now