C# 在c和mysql中重置时间和尝试
我想在达到我想要的时间时重置我表中的时间和尝试字段,例如,在限制用户重置我表中的两个字段1分钟后,我有以下方法在键入正确的用户名和密码后重置该字段:C# 在c和mysql中重置时间和尝试,c#,mysql,C#,Mysql,我想在达到我想要的时间时重置我表中的时间和尝试字段,例如,在限制用户重置我表中的两个字段1分钟后,我有以下方法在键入正确的用户名和密码后重置该字段: public bool reset_Attempt() { msc.Close(); MySqlDataReader mdr; using (MySqlCommand mcmd = new MySqlCommand()) { msc.Open(); mcmd.CommandText
public bool reset_Attempt()
{
msc.Close();
MySqlDataReader mdr;
using (MySqlCommand mcmd = new MySqlCommand())
{
msc.Open();
mcmd.CommandText = "update login_attempt set attempt=0,time=0 where hid=@hid";
mcmd.Connection = msc;
mcmd.Parameters.Add("@hid", MySqlDbType.VarChar).Value = getCPUid();
mdr = mcmd.ExecuteReader();
msc.Close();
}
return true;
}
这是我检查用户名、密码和尝试的方法:
public bool validate_Login(string username, string pass)
{
bool check = false;
MySqlDataReader mdr;
MySqlDataReader mdr2;
using (MySqlCommand mcmd2 = new MySqlCommand())
{
mcmd2.CommandText = "select hid,attempt,time from login_attempt";
mcmd2.Connection = msc;
msc.Close();
msc.Open();
mdr2 = mcmd2.ExecuteReader();
if (mdr2.Read()&&int.Parse(mdr2["attempt"].ToString()) < 4) {
using (MySqlCommand mcmd = new MySqlCommand()) {
mcmd.CommandText = "select username,password from login where binary username=@user and password=@pass";
mcmd.Connection = msc;
msc.Close();
msc.Open();
mcmd.Parameters.Add("@user", MySqlDbType.VarChar).Value = this.username=username;
mcmd.Parameters.Add("@pass", MySqlDbType.VarChar).Value = this.pass=pass;
mdr = mcmd.ExecuteReader();
if (mdr.HasRows) {
if (mdr.Read())
{
check = true;
reset_Attempt();
MessageBox.Show("correct");
}
} else {
check = false;
update_Attempt();
MessageBox.Show("incorrect");
}
}
} else if (int.Parse(mdr2["attempt"].ToString()) >= 4) {
check = false;
MessageBox.Show("You have been restrict");
set_time();
}
}
msc.Close();
return check;
}
以及我检查时间的方法:
public bool remain_AttemptsTime()
{
bool check = false;
MySqlDataReader mdr2;
using (MySqlCommand mcmd2 = new MySqlCommand())
{
mcmd2.CommandText = "select time,attempt from login_attempt WHERE time < NOW() - INTERVAL 1 MINUTE and attempt<=4";
mcmd2.Connection = msc;
msc.Close();
msc.Open();
mdr2 = mcmd2.ExecuteReader();
if (mdr2.Read()) {
check = true;
} else {
check = false;
}
}
return check;
}
现在我想在达到一分钟后重置尝试次数和时间,我该怎么做?我的算法:
void Login(string username, string password){
var user = //query db for user. Get: password, attempts = number of failed password attempts, lockedoutuntilutc = a date of when the user is locked out until; no login will ever work before this date
//if the user is currently locked out, bomb out, don't increase the failed attempts etc
if(DateTime.UtcNow < user.LockedOutUntilUtc)
throw new Exception("You're locked out. Try again in {(user.LockedOutUntilUtc - DateTime.UtcNow).TotalSeconds)} seconds");
//ok the user may make an attempt - is it correct?
if(user.Password == password.GetHashCode()){ //or better hashing algo
//correct; reset the attempt, record their successful login; leave the lockout date alone (it's in the past - who cares about it?
user.Attempts = 0;
user.LastLoginUtc = DateTime.UtcNow;
} else { //login failed
user.Attempts++; //increment fails
user.LockedOutUntilUtc = DateTime.UtcNow.AddSeconds(Math.Pow(2, user.Attempts + 1)); //lock them out for exponentially longer times the more fails they make
}
//save db
}
你可以用数学改变超时时间;进行多次失败的2^尝试+1意味着它们第一次被锁定4秒,然后是8秒、16秒、32秒、64秒。。。当他们5次搞错的时候,他们已经花了两分钟试图进去了;这是相当好的,因为它不会太惩罚一个刚刚忘记或正在尝试他们能记住的那一把的合法用户,但它会很快惩罚野蛮势力。您还可以让您的客户服务人员通过设置一个遥远的未来日期来锁定某人的帐户,或者通过给他们一个删除日期的方法来解锁他们,以便用户可以继续尝试良好的代码缩进将帮助我们阅读代码,更重要的是,它将帮助您为自己的利益调试代码。您可能会被要求在几周/几个月内修改此代码,最终您将感谢我。因此。。。嗯。。。。这里有两件非常重要的事情。首先,此密码验证代码不安全。以明文甚至加密的形式存储密码永远都是不好的。相反,必须使用不可逆的加密而不是md5哈希。当有人尝试登录时,您还可以计算尝试的密码的哈希值并比较哈希值。其他事情都很糟糕。即使是测试/学习/概念验证代码也不应该这样做。实际上,与其尝试编写自己的身份验证代码,不如更多地依赖于您选择的平台提供的身份/身份验证功能。其次,C/ADO.Net使用了一种称为连接池的功能,在整个应用程序中重复使用单个连接对象是不好的。您最好为大多数查询实例化一个新连接。我现在必须加密我的密码,我稍后会这样做,但现在我有这个问题。不,您误解了两个非常重要的点:1您不加密密码。加密还不够好。你必须散列密码,这不是一回事。2这太重要了,不能事后再做。很多时候,以后再做变成了永远不做,而这就是违规发生的时候。如何在一分钟后重置它对我的问题没有帮助?将锁定时间设置为未来一分钟!与其他类似,如果尝试次数>3,则锁定时间=now.addminutes1否我的意思是在限制尝试一分钟后,时间保存在表中,然后重新设置。这就是此代码的作用。你的想法是错误的;不要试图让计时器在一分钟内重置尝试,只允许他们在一分钟后重试。请再次阅读代码,并添加注释