Advanced Login System

Introduction

In this article we will show you how to Creating Password Recovery using Windows Application in Visual Studio using C#.

Prerequisites

Visual Studio 2010/2012/2013/15/17, SQL Server 2005/08/2012

Project used version

VS2017, SQL SERVER 2012

Password Recovery Form:

Step1: Create new Stored Procedure "spCheckEmail" and Paste the paste the below code.


USE [DBLoginSystem]
GO

/****** Object:  StoredProcedure [dbo].[spCheckEmail]    Script Date: 19-12-2018 7.03.10 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[spCheckEmail](@Email          VARCHAR(50),
                                      @Action         VARCHAR(50),
                                      @Result         VARCHAR(50) output,
                                      @QuestionOut    VARCHAR(50) output,
                                      @AnswerIn       VARCHAR(50) = NULL,
                                      @ChangePassword VARCHAR(50) = NULL)
AS
  BEGIN
      SET nocount ON;

      IF( @Action = 'CheckEmail' )
        BEGIN
            IF EXISTS(SELECT email
                      FROM   users
                      WHERE  email = @Email)
              BEGIN
                  SELECT @Result = 'Please answer the security Question.';

                  SELECT @QuestionOut = SecretQuestion
                  FROM   users
                  WHERE  email = @Email;
              END
            ELSE
              BEGIN
                  SELECT @Result = 'Your email not found.';
              END
        END
      ELSE IF( @Action = 'CheckSecurity' )
        BEGIN
            IF EXISTS(SELECT *
                      FROM   users
                      WHERE  email = @Email
                             AND SecretAnswer = @AnswerIn)
              BEGIN
                  SELECT @Result = 'Enter your password!';
              END
            ELSE
              BEGIN
                  SELECT @Result = 'Your answer is invalid.';
              END
        END
      ELSE IF( @Action = 'ChangePassword' )
        BEGIN
            UPDATE users
            SET    password = @ChangePassword, isLocked=0, RetryAttempts=0
            WHERE  email = @Email;

            SELECT @Result = 'Password changed successfully.';
        END
  END 

GO

 

Step 2: Open LINQ to SQL Classes file and Drag and drop stored procedure "Spcheckmail" from Server Explorer.

 Advance Login System-Store Procedure Check-Email

Step 3: Add new window form name it as "RecoverPassword" and design form as shown below. 

 Controls  Text   Name
 Group Box  Recover Password  grbRecoverPassword
 Group Box  Security Question and Answer  grbSecurityQuestion
 Group Box  Change Password  grbChangePassword
 Label  Enter Email Id  lblEnterEmailId
 Label  Question  lblQuestion
 Label  Answer  lblAnswer
 Label  New Password  lblNewPassword
 Label  Confirm New Password  lblConfirmPassword
TextBox    txtEmailID
TextBox    txtQuestion
 TextBox    txtAnswer
TextBox    txtNewPassword
TextBox    txtConfirmNewPassword
Button    btnRecover
Button    btnSubmit
Button    btnChangePassword

 

Advance Login System_Recover Password

Step 4: Add the following code on form that hides group box (Security Question and Answer, Change Password) initially.

 
private void RecoverPassword_Load(object sender, EventArgs e)
        {
            groupBox2.Hide();
            groupBox3.Hide();
        }

Step 5: Declare variable result and question as public string as shown below.

 Advance Login System-Decleared variables

Step 6: Double Click on Recover button. Paste the following code on onClick event.

 
private void BtnRecover_Click(object sender, EventArgs e)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                db.spCheckEmail(txtEmailID.Text, "CheckEmail", ref result, ref question, txtAnswer.Text, txtConfirmNewPassword.Text);
                db.SubmitChanges();
                MessageBox.Show(result);
                if (result == "Answer Security Question")
                {
                    txtQuestion.Text = question;
                    groupBox2.Show();
                    groupBox3.Hide();
                }
                else
                {
                    txtQuestion.Text = "";
                    groupBox2.Hide();
                    groupBox3.Hide();
                }
            }
        }

Step 7: Double Click on Submit button. Paste the following code on onClick event.


private void BtnSubmit_Click(object sender, EventArgs e)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                db.spCheckEmail(txtEmailID.Text, "CheckSecurity", ref result, ref question, txtAnswer.Text, txtConfirmNewPassword.Text);
                db.SubmitChanges();
                MessageBox.Show(result);
                if (result == "Enter new Password !!!")
                {
                    txtQuestion.Text = question;
                    groupBox3.Show();
                }
                else
                {
                    txtQuestion.Text = "";
                    groupBox3.Hide();
                }
            }
        }

Step 8: Double Click on Change Password button. Paste the following code on onClick event.


private void BtnChangePassword_Click(object sender, EventArgs e)
        {
            if (txtNewPassword.Text == txtConfirmNewPassword.Text)
            {
                using (DataClassesDataContext db = new DataClassesDataContext())
                {
                    db.spCheckEmail(txtEmailID.Text, "ChangePassword", ref result, ref question, txtAnswer.Text, txtConfirmNewPassword.Text);
                    db.SubmitChanges();
                    MessageBox.Show(result);
                    txtEmailID.Text = "";
                    txtAnswer.Text = "";
                    txtNewPassword.Text = "";
                    txtConfirmNewPassword.Text = "";
                    txtQuestion.Text = "";
                    groupBox2.Hide();
                    groupBox3.Hide();
                }
            }
            else
            {
                MessageBox.Show("Password does not matching");
            }
        }

Complete Source Code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AdvancedLoginSystem
{
    public partial class frmRecoverPassword : Form
    {
        public string result;
        public string question;
        public frmRecoverPassword()
        {
            InitializeComponent();
        }

        private void RecoverPassword_Load(object sender, EventArgs e)
        {
            groupBox2.Hide();
            groupBox3.Hide();
        }

        private void BtnRecover_Click(object sender, EventArgs e)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                db.spCheckEmail(txtEmailID.Text, "CheckEmail", ref result, ref question, txtAnswer.Text, txtConfirmNewPassword.Text);
                db.SubmitChanges();
                MessageBox.Show(result);
                if (result == "Please answer the security Question.")
                {
                    txtQuestion.Text = question;
                    groupBox2.Show();
                    groupBox3.Hide();
                }
                else
                {
                    txtQuestion.Text = "";
                    groupBox2.Hide();
                    groupBox3.Hide();
                }
            }
        }

        private void BtnSubmit_Click(object sender, EventArgs e)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                db.spCheckEmail(txtEmailID.Text, "CheckSecurity", ref result, ref question, txtAnswer.Text, txtConfirmNewPassword.Text);
                db.SubmitChanges();
                MessageBox.Show(result);
                if (result == "Enter new Password !!!")
                {
                    txtQuestion.Text = question;
                    groupBox3.Show();
                }
                else
                {
                    txtQuestion.Text = "";
                    groupBox3.Hide();
                }
            }
        }

        private void BtnChangePassword_Click(object sender, EventArgs e)
        {
            if (txtNewPassword.Text == txtConfirmNewPassword.Text)
            {
                using (DataClassesDataContext db = new DataClassesDataContext())
                {
                    db.spCheckEmail(txtEmailID.Text, "ChangePassword", ref result, ref question, txtAnswer.Text, txtConfirmNewPassword.Text);
                    db.SubmitChanges();
                    MessageBox.Show(result);
                    txtEmailID.Text = "";
                    txtAnswer.Text = "";
                    txtNewPassword.Text = "";
                    txtConfirmNewPassword.Text = "";
                    txtQuestion.Text = "";
                    groupBox2.Hide();
                    groupBox3.Hide();
                }
            }
            else
            {
                MessageBox.Show("Password does not matching");
            }
        }
    }
}

 

Step 9: Goto Login form and double click on forgot password button and paste the following code to navigate form from Login to Forgot Password Form.


private void btnForgotPassword_Click(object sender, EventArgs e)
        {
            frmRecoverPassword recpass = new frmRecoverPassword();
            recpass.Show();
        }
 

Step 10: Run Program and click the forget password then, 
Input your register Email ID and click Recover button (Initially other group box hidden).

Advance Login System-frmRecovery Password

If Email ID does not exist then returns "Invalid Email Id"

else

Continues and ask for Security Answer that corresponding to question.

Advance Login System-Answer Scurity Question

If security answers matches then you can change password. 

Advance Login System-Password Changed Sucessfully

 

Download Complete Source Code C#


 

 

Video Tutorial