A Simple Login Form for C#

tinstaafl 0 Tallied Votes 7K Views Share

This is a very simple login form. I set it up so that the form won't close until it's cancelled or a valid username and password are entered. I chained the check for the username and the check for the password, so that if the username doesn't pass the password isn't checked and the password check returns false. The code is simple enough that that is easily changed

I included a picturebox for a logo.

This is the code inside the form:
    
    	using System;
    	using System.Collections.Generic;
    	using System.ComponentModel;
    	using System.Drawing;
    	using System.Windows.Forms;
    	
    	namespace LoginDialogForm
    	{
    		public partial class Login_Dialog_Form1 : Form
    		{
    			public Login_Dialog_Form1()
    			{
    				InitializeComponent();
    			}
    			private bool ValidateUsername()
    			{
    				//TODO: add code to validate User Name.
    				return true;
    			}
    			private bool ValidatePassword()
    			{
    				if (!ValidateUsername())
    				{
    					MessageBox.Show("Wrong Username", "Invalid Username", MessageBoxButtons.OK, MessageBoxIcon.Error);
    					return false;
    				}
    				else
    				{
    					//TODO: add code to validate password.
						if (false)
    					{
    						MessageBox.Show("Wrong Password", "Invalid Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
    						return false;
    					}
    					else
    						return true;
    					}
    				}
    			}
    			private void btnOk_Click(object sender, EventArgs e)
    			{
    				if (!ValidatePassword())
    				{
    					txtUserName.Clear();
    					txtPassword.Clear();                
    					return;
    				}
    				else
    				{
    					this.DialogResult = DialogResult.OK;
    					this.Close();
    				}
    			}
    	
    			private void btnCancel_Click(object sender, EventArgs e)
    			{
    				txtUserName.Clear();
    				txtPassword.Clear();
    				this.Close();
    			}
    		}
    	}
    
This the controls and their relevant properties:
    
                // 
                // btnOk
                // 
                Name = "btnOk";
                Text = "&Ok";
               	btnOk.Click += new System.EventHandler(this.btnOk_Click);
                // 
                // btnCancel
                // 
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
                Name = "btnCancel";
                Text = "&Cancel";
                btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
                // 
                // txtUserName
                // 
                Name = "txtUserName";
                // 
                // txtPassword
                // 
                PasswordChar = '*';
    			Name = "txtPassword";
                // 
                // label1
                // 
                Name = "label1";
                Text = "Username";
                // 
                // label2
                // 
                Name = "label2";
                Text = "Password";
                // 
                // LogoPictureBox
                // 
                LogoPictureBox.Name = "LogoPictureBox";
                LogoPictureBox.TabStop = false;
                // 
                // LoginForm1
                // 
                AcceptButton = this.btnOk;
                CancelButton = this.btnCancel;
                ControlBox = false;
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
                Name = "LoginForm1";
                ShowInTaskbar = false;
                StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                Text = "Login Form";
    			
This the code to call the form:
    
            private void Form1_Load(object sender, EventArgs e)
            {
                    
                    Login_Dialog_Form1 NewLogin = new Login_Dialog_Form1();                
                    DialogResult Result = NewLogin.ShowDialog();
                    switch (Result)
                    {
                        case DialogResult.OK:
                            //do stuff
                            break;
                        case DialogResult.Cancel:
                            this.Close();
                            break;
                    }
            }
tinstaafl 1,176 Posting Maven

I attached the template archive. If you copy that file to the My Documents\Visual Studio 2010\Temnplates\Item Templates folder, it will be in the New Items dialog, so that it is easy to add to any project.

Menns 0 Newbie Poster

how can u secure password by hashing it in the database and verify it during login

tinstaafl 1,176 Posting Maven

Welcome to Daniweb. You should ask this as a new question, instead of piggybacking on this post. Also it would probably help to search existing posts first, to see if the answer someone else received will help you.

jireh 3 Posting Whiz

You can get a sample from creating a new project and add the login form.

PulsarScript 0 Junior Poster

Where is the password is stored and is there any validation on password?

lxXTaCoXxl 26 Posting Whiz in Training

Validation assembly required?

tinstaafl 1,176 Posting Maven

Yes. Since the actual validation will vary by each person's requirements I left those as TODO's in the code.

lxXTaCoXxl 26 Posting Whiz in Training

Why not add in a class for authentication and allow for modification through properties. Even if the class doesn't have everything needed for that particular case the user could always add what they need during implementation.

tinstaafl 1,176 Posting Maven

The idea is for a simple login form template similar to what Microsoft did for VB.net. Basically it follows the KISS principle. The user is free to expand however he/she would like.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.