简体   繁体   中英

How to show password recovery form after user enters invalid credentials three times

hi evryone can you help me please.. i have a code on my windows form application using visual studio community 2015 I have a textBox the name is username the other one is password and the last is login if the user want to login and it will get a tree times error here the problem I want it automatically show the forgot password and how to recover password if the user is tree times error when he or she login the form. before I'll go to my database form i have no idea can you help please and explain how.

here is my 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 SQLSERVER_VISUALSTUDIO_COMMUNITY
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            txt_Password.PasswordChar = '*';
        }

        private void txt_login_Click(object sender, EventArgs e)
        {
            if (txt_USername.Text == "" && txt_Password.Text == "")
            {
                MessageBox.Show("Please enter your password and user_name");
                txt_USername.Clear();
                txt_Password.Clear();
            }
            else if (txt_USername.Text == "jondygonzales" && txt_Password.Text == "sharkwebcaster")
            { 
                MessageBox.Show("successfully log_in");
                Form1 f = new Form1();
                f.Show();
                Form2 main = new Form2();
                main.Show();
                this.Hide(); 
            }
        }
    }
}

What you need to do is keep a counter which you will increase by one when username and password are not valid.

You check this variable value and when it reaches 3, you show user the password recovery form.

public partial class Form1 : Form
{
    int loginAttemps = 0;
    public Form1()
    {
        InitializeComponent();
        txt_Password.PasswordChar = '*';
    }

    private void txt_login_Click(object sender, EventArgs e)
    {
        if (txt_USername.Text == "" && txt_Password.Text == "")
        {
            MessageBox.Show("Please enter your password and user_name");
            txt_USername.Clear();
            txt_Password.Clear();
        }
        else if (txt_USername.Text == "jondygonzales" && txt_Password.Text == "sharkwebcaster")
        { 
            loginAttempts = 0;
            MessageBox.Show("successfully log_in");
            Form1 f = new Form1();
            f.Show();
            Form2 main = new Form2();
            main.Show();
            this.Hide(); 
        }
        else
        {
            loginAttempts += 1;

            if(loginAttemps == 3)
            {
                RecoveryForm recForm = new RecoveryForm(); // You need to use correct Form here.
                recForm.Show();
                this.Hide();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM