猿问

用户尝试使用计数

我想使用条件在我的计数中创建用户尝试。我如何使用条件来执行这种逻辑以及在下面的代码中放置的位置。示例:如果用户尝试登录 3 次,但用户输入错误并通过,他将收到一条消息,提示您已达到登录尝试次数


private void btn_login_Click(object sender, EventArgs e)

{

        var obj = new Usercontrols.SIMSMain();

        obj.Dock = DockStyle.Fill;


        conn.Open();

        SqlCommand selectCommand = new SqlCommand("Select * from admin_access where Username=@admin AND Password=@eyelab",conn);

        selectCommand.Parameters.AddWithValue("@admin", txt_username.Text);

        selectCommand.Parameters.AddWithValue("@eyelab", txt_password.Text);

        SqlDataReader dataReader;   

        dataReader = selectCommand.ExecuteReader();

        var count = 0;


        while (dataReader.Read())

        {

            count = count + 1;

        }

    if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))

    {

        MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

    }

    else

    {

        if (count == 1)

        {

            MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.Hide();

            this.Parent.Controls.Add(obj);

        }

        else if (count == 3)

        {

            count++;

            MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else

        {

            MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);

        }    

    }

    conn.Close();

}


森林海
浏览 216回答 3
3回答

慕哥9229398

这是您修改后的代码,用于处理超过 3 次的尝试Timer loginAttempsTimeOut;Dictionary<string, int> loginAttempsPerUser = new Dictionary<string,int>();Dictionary<string, DateTime> loginAttemptsViolated = new Dictionary<string, DateTime>();int TimeOutInMinutes = 15;private void SetTimer(){&nbsp; &nbsp; loginAttempsTimeOut = new Timer();&nbsp; &nbsp; loginAttempsTimeOut.Interval = 1000 * 60; // check timeout every 1 mins&nbsp; &nbsp; loginAttempsTimeOut.Enalbed = true;&nbsp; &nbsp; loginAttempsTimeOut.Tick += LoginAttempsTimeOut_Tick;}// set a timer, and if login timeout for each user is elapsed,// allow user to try login againprivate void LoginAttempsTimeOut_Tick(object sender, EventArgs e){&nbsp; &nbsp; foreach(var user in loginAttemptsViolated.Keys)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; loginAttemptsViolated.TryGetValue(user, out var date);&nbsp; &nbsp; &nbsp; &nbsp; TimeSpan span = DateTime.Now.Subtract(date);&nbsp; &nbsp; &nbsp; &nbsp; if(span.TotalMinutes > TimeOutInMinutes)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loginAttempsPerUser[user] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loginAttemptsViolated.Remove(user);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loginAttempsPerUser.Remove(user);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}private void btn_login_Click(object sender, EventArgs e){&nbsp; &nbsp; &nbsp; &nbsp; var obj = new Usercontrols.SIMSMain();&nbsp; &nbsp; &nbsp; &nbsp; obj.Dock = DockStyle.Fill;&nbsp; &nbsp; &nbsp; &nbsp; conn.Open();&nbsp; &nbsp; &nbsp; &nbsp; SqlCommand selectCommand = new SqlCommand("Select * from admin_access where Username=@admin AND Password=@eyelab", conn);&nbsp; &nbsp; &nbsp; &nbsp; selectCommand.Parameters.AddWithValue("@admin", txt_username.Text);&nbsp; &nbsp; &nbsp; &nbsp; selectCommand.Parameters.AddWithValue("@eyelab", txt_password.Text);&nbsp; &nbsp; &nbsp; &nbsp; SqlDataReader dataReader;&nbsp; &nbsp; &nbsp; &nbsp; dataReader = selectCommand.ExecuteReader();&nbsp; &nbsp; &nbsp; &nbsp; var count = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (dataReader.Read())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = count + 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(loginAttemptsViolated.ContainsKey(txt_username.Text))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MetroMessageBox.Show("Login attempts is more than 3.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Hide();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Parent.Controls.Add(obj);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (count == 3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if user cannot login, increase login attempts&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!loginAttempsPerUser.ContainsKey(txt_username.Text))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loginAttempsPerUser.Add(txt_username.Text, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loginAttempsPerUser[txt_username.Text]++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(loginAttempsPerUser[txt_username.Text] > 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if login attempts > 2 set a 15 min timeout till user&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // cant login&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!loginAttemptsViolated.ContainsKey(txt_username.Text))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loginAttemptsViolated.Add(txt_username.Text, DateTime.Now);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; conn.Close();&nbsp; &nbsp; }}也有超时,因为如果用户违反了登录计数,则必须稍后再给他/她一次机会(例如 15 分钟后)。

至尊宝的传说

您需要将计数变量声明为全局变量。&nbsp; &nbsp; int count = 1;&nbsp; &nbsp; private void btn_login_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var obj = new Usercontrols.SIMSMain();&nbsp; &nbsp; &nbsp; &nbsp; obj.Dock = DockStyle.Fill;&nbsp; &nbsp; &nbsp; &nbsp; conn.Open();&nbsp; &nbsp; &nbsp; &nbsp; SqlCommand selectCommand = new SqlCommand("Select * from admin_access where Username=@admin AND Password=@eyelab", conn);&nbsp; &nbsp; &nbsp; &nbsp; selectCommand.Parameters.AddWithValue("@admin", txt_username.Text);&nbsp; &nbsp; &nbsp; &nbsp; selectCommand.Parameters.AddWithValue("@eyelab", txt_password.Text);&nbsp; &nbsp; &nbsp; &nbsp; SqlDataReader dataReader;&nbsp; &nbsp; &nbsp; &nbsp; dataReader = selectCommand.ExecuteReader();&nbsp; &nbsp; &nbsp; &nbsp; var counter = 0; //to check if there is data&nbsp; &nbsp; &nbsp; &nbsp; while (dataReader.Read())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (counter == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Hide();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Parent.Controls.Add(obj);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (count == 3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; conn.Close();&nbsp; &nbsp; }

红糖糍粑

您已经在使用SqlDataReader.Read()简单的 else 可以处理没有结果返回的情况。您已经在使用SqlDataReader.Read()简单的 else 可以处理没有结果返回的情况。int failAttempt&nbsp; = 0;private void btn_login_Click(object sender, EventArgs e){&nbsp; &nbsp; // Step 1: Check if inputs are valid&nbsp; &nbsp; if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; // Step 2: Check if there is one user with this password/login&nbsp; &nbsp; var obj = new Usercontrols.SIMSMain();&nbsp; &nbsp; obj.Dock = DockStyle.Fill;&nbsp; &nbsp; conn.Open();&nbsp; &nbsp; SqlCommand selectCommand = new SqlCommand("Select * from admin_access where Username=@admin AND Password=@eyelab",conn);&nbsp; &nbsp; selectCommand.Parameters.AddWithValue("@admin", txt_username.Text);&nbsp; &nbsp; selectCommand.Parameters.AddWithValue("@eyelab", txt_password.Text);&nbsp; &nbsp; SqlDataReader dataReader = selectCommand.ExecuteReader();&nbsp; &nbsp; int numberMatch=0;&nbsp; &nbsp; while(dataReader.Read()){&nbsp; &nbsp; &nbsp; &nbsp; numberMatch++;&nbsp; &nbsp; }&nbsp; &nbsp; conn.Close();&nbsp; &nbsp; // Step 3: Fail Handle&nbsp; &nbsp; if(numberMatch==1){ // Success&nbsp; &nbsp; &nbsp; &nbsp; failAttempt&nbsp; = 0;&nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);&nbsp; &nbsp; &nbsp; &nbsp; this.Hide();&nbsp; &nbsp; &nbsp; &nbsp; this.Parent.Controls.Add(obj);&nbsp; &nbsp; }&nbsp; &nbsp; else { // Fail 0 or more than one&nbsp; &nbsp; &nbsp; &nbsp; failAttempt ++;&nbsp; &nbsp; &nbsp; &nbsp; if (failAttempt == 3)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答