繁体   English   中英

如何在登录屏幕上创建登录限制?

[英]How do I create a limit of logins on my login screen?

我目前正在尝试用C#设计一个atm机器,对此我还是很陌生。 在尝试登录失败3次后,我希望登录屏幕返回到我的欢迎屏幕,但是我不知道从哪里开始以及如何在我的程序中实现我的代码。

我当前的登录屏幕代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Common;

namespace bankkk
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        public static OleDbConnection con = new OleDbConnection();

        string dbProvider;
        string dbSource;

        OleDbDataAdapter da;

        public static DataSet ds1 = new DataSet();

        string sql;
        string pin;
        int rownum = 0;
        bool valid = false;

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;";
            dbSource = "Data Source = 'd:\\bank11.accdb'";
            con.ConnectionString = dbProvider + dbSource;
            ds1 = new DataSet();
            con.Open();
            sql = " SELECT tblCustomers.* FROM tblCustomers";
            da = new OleDbDataAdapter(sql, con);
            rownum = da.Fill(ds1, "tblCustomers");

            con.Close();
        }

        private void btnexit_Click(object sender, EventArgs e)
        {

            System.Environment.Exit(0);
            this.Close();

        }


        //METHOD VALIDATE

        private bool validate()
        {
            ds1 = new DataSet();
            con.Open();

            sql = "SELECT tblCustomers.* FROM tblCustomers WHERE ((tblCustomers.AccountNo) = '" + txtAccount.Text + "')";
            da = new OleDbDataAdapter(sql, con);
            rownum = da.Fill(ds1, "tblCustomers");
            con.Close();

            if (rownum != 1)
            {
                MessageBox.Show("Not a valid Account");
                return false;
            }
            else
            {
                pin = ds1.Tables["tblCustomers"].Rows[0][4].ToString();
                if (pin == txtPin.Text)
                {
                    return true;
                }
                else
                {
                    MessageBox.Show("INVALID PIN");
                    return false;
                }
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            valid = validate();
            if (valid == true)
            {
                if (txtAccount.Text == "11111111" && txtPin.Text == "9999")
                {
                    Frmmanager Manager = new Frmmanager();
                    this.Close();
                    Manager.Show();
                }
                else
                {
                    frmaccount account = new frmaccount();
                    this.Close();
                    account.Show();

                    {
                        txtAccount.Clear();
                        txtPin.Clear();
                    }
                }
            }
        }

        private void btnlogin_Click_1(object sender, EventArgs e)
        {
            valid = validate();
            if (valid == true)
            {
                if (txtAccount.Text == "11111111" && txtPin.Text == "9999")
                {
                    Frmmanager Manager = new Frmmanager();
                    this.Close();
                    Manager.Show();
                }
                else
                {
                    frmaccount account = new frmaccount();
                    this.Close();
                    account.Show();

                    {
                        txtAccount.Clear();
                        txtPin.Clear();
                    }
                }
            }
        }
    }
}

您说的是返回到我的欢迎屏幕 ,所以我假设您正在使用.ShowDialog()而不是.Show()显示FrmLogin 这样,您只需要关闭旧表格即可。

如果您只是在执行.Show() ,则可以执行以下操作:

public partial class FrmWelcome {

    //in some part of your code...
    var frmLogin = new FrmLogin();

    //when the login form is closed, show this one.
    //Depending on your application, you might want to add some boolean variable
    //to the Login Form that will be true if the user authentication failed, and 
    //show this form again only if it is true.
    frmLogin.Closed += (s, e) => this.Show();

    this.Hide();
    frmLogin.Show();
}

试试下面的代码。 这个想法是有一个failedAttempts变量,每次验证代码失败时该变量都会增加。 当它等于3时,您只需关闭窗体(请参见上文)。

namespace bankkk
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        ...

        int failedAttempts = 0;

        private void btnlogin_Click_1(object sender, EventArgs e)
        {
            valid = validate();
            if (!valid) {
                //Increment the number of failed attempts
                failedAttempts += 1;

                //If equal to 3
                if (failedAttempts == 3) {
                    //Just close this window
                    this.Close();
                }
            }
            else
            {
                //the same code you were using...
            }
        }
    }
}

您可以在程序中添加一个变量,然后增加它以限制登录的尝试次数。 要限制帐户的尝试次数,请添加一个表以将登录信息(包括无效尝试)存储到数据库中。 将该表链接到您的客户表。 尝试登录无效的客户帐户时,尝试增加无效尝试的次数,然后将其写回到登录表中。 成功登录后,您可以将无效尝试次数设置为0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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