我想使用条件创建一个用户尝试计数。我如何使用条件以及在下面的代码中放置位置的方式执行这种逻辑。 示例:如果用户尝试使用错误的用户输入登录3次并通过,则他将收到一条消息,提示您已经达到登录尝试
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n=3;
int a[10]={ -500, 2147483647, 2147483647};
int t = upper_bound(a,a+n,2147483647)-a;
int k = lower_bound(a,a+n,2147483647+1)-a;
cout << "upper_bound: " << t << " " << a[t]<<endl;
cout << "lower_bound: " << k << " " << a[k]<<endl;
return 0;
}
答案 0 :(得分:0)
您已经在使用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);
}
}
答案 1 :(得分:0)
在这里修改代码,以处理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分钟后)。
答案 2 :(得分:0)
您需要将计数变量声明为全局变量。
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();
}
答案 3 :(得分:-1)
您必须声明一个静态计数器,该计数器随着每次用户登录单击而增加。如果同一用户正在尝试并且计数器为3,则显示消息,例如“您已超出限制”。下面是修改后的代码。
private static int loginCounter;
private static string UserName;
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 == 0 && UserName == txt_username.Text)
{
loginCounter += 1;
}
if(loginCounter >= 3)
{
MetroMessageBox.Show(this, "You have exceeded maximum login attempts.", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
UserName == txt_username.Text
if (count == 1)
{
// Here reset the login counter, since the password is right
loginCounter = 0;
UserName = "";
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();
}