How to Create a Simple Login System in C#

In this tutorial, I will teach you how to create a simple login system in c#. This simple login system is a good start when you begin to program for the security of your application. This has a default username and password that can be used when you log in. See the procedure below.

Creating application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#. After that, do the form just like shown below. login123

Step 2

Double click the form and hide the “Logout” button and “username” label in the first load of the form.
  1.             lblUsername.Visible = false;
  2.             button2.Visible = false;

Step 3

Go back to the design views, double click the “Login” button and do the following codes in the click event handler of a button to fire the method when its clicked.
  1.             string str_username;
  2.             string str_password;
  3.             str_username = "itsourcecode.com";
  4.             str_password = "admin";
  5.  
  6.             if(textBox1 .Text == str_username && textBox2 .Text == str_password)
  7.             {
  8.                 MessageBox.Show("Login successfully");
  9.  
  10.                 lblUsername.Visible = true;
  11.                 button2.Visible = true;
  12.                 lblUsername.Text ="Howdy, " + str_username;
  13.  
  14.                 label1.Visible = false;
  15.                 label2.Visible = false;
  16.                 textBox1.Visible = false;
  17.                 textBox2.Visible = false;
  18.                 button1.Visible = false;
  19.             }else
  20.             {
  21.                 MessageBox.Show("Account does not exist.");
  22.             }

Step 4

Do the following code for the “Logout” button.
  1.         private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             label1.Visible = true;
  4.             label2.Visible = true;
  5.             textBox1.Visible = true;
  6.             textBox2.Visible = true;
  7.             button1.Visible = true;
  8.  
  9.             lblUsername.Visible = false;
  10.             button2.Visible = false;
  11.  
  12.             textBox1.Text = "";
  13.             textBox2.Text = "";
  14.             textBox1.Focus();
  15.  
  16.  
  17.         }
The complete source code is included. You can download it and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT FB Account – https://www.facebook.com/onnaj.soicalap

Add new comment