Answer the question
In order to leave comments, you need to log in
How to implement an action after n-seconds (see inside)?
The assignment says:
If the username/password was entered incorrectly for 3 times in a row, then the system should be blocked for the next 15 seconds for the first time, with an increase in the blocking time by 20 seconds for each subsequent incorrect block.
int WrongCounts = 0;
using (var db = new WorldYachtsEntities())
{
//Ищем пользователя в базе
var user = db.Users.AsNoTracking().FirstOrDefault(u => u.Login == LoginBox.Text && u.Password == PasswordBox.Text);
//Если нет
if (user == null)
{
WrongCounts++;
if (WrongCounts == 3)
{
LoginWarnLabel.Text = "Заблокировано на 15 секунд";
LoginBtn.Enabled = false;
}
LoginWarnLabel.Text = "Неверная пара логин/пароль\nНецдачных попыток: " + WrongCounts;
return;
}
//Если есть
switch (user.UserGroup) //Проверяем группу
{
case "admin":
GenForms.AdminForm();
break;
case "customer":
GenForms.CustomerForm();
break;
}
}
Answer the question
In order to leave comments, you need to log in
Since everything is a crutch, then
Thread.Sleep(15000);
But in general, remember the time and when you press the button, check whether enough time has passed.
There are essentially exactly two approaches.
First - if you need the program to unlock itself - then you need a timer. Usually for any forms there is a "timer" component. Well, you are tied there to the time and after the expiration you unlock the form.
Second. The program itself does not change anything in its state. However, when you try to authorize if the time has not expired, then a message is displayed about the time blocking. Everything is simpler here - you need a variable when possible in the next. log in once, and in the authorization first compare it with the current time
Let's start by thinking of Authorization as a stateful service. And forward the service instance to the form class as a dependency. In this way, Authorization will be testable, which ensures that what is described in the requirements is implemented as expected. In the state of the service, you can include the Time of the Last Failed Authorization Attempt and the Number of Failed Authorizations, which are reset upon successful auth. The state can be stored next to the user/pass data or in an in-memory application. The service method code itself becomes simpler. At least if + switch, at least the state machine will tell you like a fantasy, but already without timers. Denial of authorization to implement through throw a specific exception, which you catch on the form and paint as you please.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question