c# - 使用计数的用户尝试

标签 c# visual-studio winforms

我想使用条件在我的计数中创建一个用户尝试。我如何使用条件以及在下面的代码中放置这种逻辑。 示例:如果用户尝试登录 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();
}

最佳答案

这是您修改后的代码,用于处理超过 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()
{
    loginAttempsTimeOut = new Timer();
    loginAttempsTimeOut.Interval = 1000 * 60; // check timeout every 1 mins
    loginAttempsTimeOut.Enalbed = true;
    loginAttempsTimeOut.Tick += LoginAttempsTimeOut_Tick;
}

// set a timer, and if login timeout for each user is elapsed,
// allow user to try login again
private void LoginAttempsTimeOut_Tick(object sender, EventArgs e)
{
    foreach(var user in loginAttemptsViolated.Keys)
    {
        loginAttemptsViolated.TryGetValue(user, out var date);
        TimeSpan span = DateTime.Now.Subtract(date);
        if(span.TotalMinutes > TimeOutInMinutes)
        {
            loginAttempsPerUser[user] = 0;
            loginAttemptsViolated.Remove(user);
            loginAttempsPerUser.Remove(user); 
        }
    }
}

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(loginAttemptsViolated.ContainsKey(txt_username.Text))
        {
           MetroMessageBox.Show("Login attempts is more than 3.");
        }
        else 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
            {
                //if user cannot login, increase login attempts
                if(!loginAttempsPerUser.ContainsKey(txt_username.Text))
                   loginAttempsPerUser.Add(txt_username.Text, 1);

                loginAttempsPerUser[txt_username.Text]++;
                if(loginAttempsPerUser[txt_username.Text] > 2)
                {
                    // if login attempts > 2 set a 15 min timeout till user 
                    // cant login
                    if(!loginAttemptsViolated.ContainsKey(txt_username.Text))
                       loginAttemptsViolated.Add(txt_username.Text, DateTime.Now);
                }
                MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
        conn.Close();
    }

}

也有超时,因为如果用户违反登录计数,则必须稍后再给他/她一次机会(例如 15 分钟后)。

关于c# - 使用计数的用户尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666379/

相关文章:

c# - 非永久性地删除数据库记录(软删除)

c# - 从 RichTextBox 保存 RTF 时丢失表格宽度自动调整大小

asp.net - 在asp.net项目中使用vs-threading

c# - 如何让Windows窗体应用程序自动运行?

c# - 将新对象实例分配给绑定(bind)变量时数据绑定(bind)不起作用

c# - Regex.replace 不会用开始字符替换多行文本行到 html block

JavaScript 倒计时与服务器时间同步

c++ - 我所有变量的运行时检查失败 #2 错误

c# - 我可以在单元测试中写入控制台吗?如果是,为什么控制台窗口不打开?

c# - 如何在DataGridView中搜索没有数据库的文件