3 回答
TA贡献1877条经验 获得超6个赞
这是您修改后的代码,用于处理超过 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 分钟后)。
TA贡献1789条经验 获得超10个赞
您需要将计数变量声明为全局变量。
int count = 1;
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 counter = 0; //to check if there is data
while (dataReader.Read())
{
counter++;
}
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 (counter == 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
{
count++;
MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
conn.Close();
}
TA贡献1815条经验 获得超6个赞
您已经在使用SqlDataReader.Read()简单的 else 可以处理没有结果返回的情况。您已经在使用SqlDataReader.Read()简单的 else 可以处理没有结果返回的情况。
int failAttempt = 0;
private void btn_login_Click(object sender, EventArgs e)
{
// Step 1: Check if inputs are valid
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);
return;
}
// Step 2: Check if there is one user with this password/login
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 = selectCommand.ExecuteReader();
int numberMatch=0;
while(dataReader.Read()){
numberMatch++;
}
conn.Close();
// Step 3: Fail Handle
if(numberMatch==1){ // Success
failAttempt = 0;
MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
this.Parent.Controls.Add(obj);
}
else { // Fail 0 or more than one
failAttempt ++;
if (failAttempt == 3)
{
MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
- 3 回答
- 0 关注
- 215 浏览
添加回答
举报