您好我在c#windows窗体中有类似的登录系统。当用户输入3次错误的详细信息时,会显示一条消息并说出已锁定的帐户。是否有任何技巧/方法可以冻结该用户的帐户几秒钟?
答案 0 :(得分:1)
您可以创建一个词典,在他被阻止时提供用户名和时间。 e.g:
private Dictionary<string, DateTime> blockedUsers;
登录前,您将验证当前用户是否存在于“blockedUsers”中,如果为true,则必须比较当前时间和该特定用户列表中存储的时间。如果它在30秒的范围之间,您可以取消显示错误的登录。否则,您将从阻止列表中删除此用户。像这样:
// Checks if the user exists in the blockedUsers.
if (blockedUsers.ContainsKey(userName))
{
// If so, then gets the difference between when he was blocked and now.
var diffInSeconds = (DateTime.Now - blockedUsers[userName]).TotalSeconds;
// If the difference is smaller than 30, prevent him from loggin.
if (diffInSeconds < 30)
{
MessageBox.Show("Sorry, but your user has been temporary blocked from loggin. Try later.");
return;
}
// If the diff is greater than 30, then there is no reason to keep him in blocked list.
else
{
blockedUsers.Remove(userName);
}
}
你需要做的其他事情是:如果“wrongAttempts”是3,那么你将它添加到“blockedUsers”中,如果他试图在30秒内再次登录,他将被第一次验证阻止。 / p>
if (wrongAttempts >= 3)
{
blockedUsers.Add(userName, DateTime.Now);
}
就是这样。希望它可以帮到你!
答案 1 :(得分:0)
冻结帐户有很多方法。您可以设置权限列表。在权限列表中,您可以设置一个令牌,并在每次有人尝试登录时递增它。如果令牌的值超过某个值,则用户无法在DateTime
设置确定的某个时间段内登录。
如果有的话,你有什么尝试?您是否有可以共享的代码,以便用户了解您希望进入的方向?