Initial commit: demo 24.11
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
using System.Drawing.Drawing2D;
|
||||
using RentalApp.Data;
|
||||
using RentalApp.Helpers;
|
||||
|
||||
namespace RentalApp.Forms;
|
||||
|
||||
public class LoginForm : Form
|
||||
{
|
||||
private const int FAIL_LOCK_SECONDS = 10;
|
||||
|
||||
// --- контролы ---
|
||||
private readonly Label lblTitle = new();
|
||||
private readonly Label lblLogin = new();
|
||||
private readonly TextBox txtLogin = new();
|
||||
private readonly Label lblPassword = new();
|
||||
private readonly TextBox txtPassword = new();
|
||||
private readonly CheckBox chkShowPassword = new();
|
||||
private readonly Label lblCaptcha = new();
|
||||
private readonly PictureBox picCaptcha = new();
|
||||
private readonly TextBox txtCaptcha = new();
|
||||
private readonly Button btnRefreshCaptcha = new();
|
||||
private readonly Button btnLogin = new();
|
||||
private readonly Label lblError = new();
|
||||
|
||||
// --- состояние ---
|
||||
private int _failedAttempts = 0;
|
||||
private bool _captchaRequired = false;
|
||||
private DateTime? _captchaLockUntil = null;
|
||||
private string _currentCaptcha = "";
|
||||
private readonly System.Windows.Forms.Timer _attemptTimer = new();
|
||||
|
||||
public LoginForm()
|
||||
{
|
||||
BuildUi();
|
||||
|
||||
if (LockHelper.IsLocked())
|
||||
{
|
||||
btnLogin.Enabled = false;
|
||||
txtLogin.Enabled = false;
|
||||
txtPassword.Enabled = false;
|
||||
lblError.Text = "Вход заблокирован. Попробуйте позже.";
|
||||
}
|
||||
|
||||
txtPassword.UseSystemPasswordChar = true;
|
||||
chkShowPassword.CheckedChanged += (s, e) =>
|
||||
txtPassword.UseSystemPasswordChar = !chkShowPassword.Checked;
|
||||
|
||||
SetCaptchaVisible(false);
|
||||
|
||||
_attemptTimer.Interval = 500;
|
||||
_attemptTimer.Tick += AttemptTimer_Tick;
|
||||
|
||||
btnLogin.Click += BtnLogin_Click;
|
||||
btnRefreshCaptcha.Click += (s, e) => GenerateCaptcha();
|
||||
txtCaptcha.KeyDown += (s, e) => { if (e.KeyCode == Keys.Enter) BtnLogin_Click(s, e); };
|
||||
txtPassword.KeyDown += (s, e) => { if (e.KeyCode == Keys.Enter) BtnLogin_Click(s, e); };
|
||||
|
||||
GenerateCaptcha();
|
||||
UpdateLockUI();
|
||||
}
|
||||
|
||||
private void BuildUi()
|
||||
{
|
||||
lblTitle.AutoSize = true;
|
||||
lblTitle.Font = new Font("Segoe UI", 14F, FontStyle.Bold);
|
||||
lblTitle.Location = new Point(30, 20);
|
||||
lblTitle.Text = "Вход в систему «Приэльбрусье»";
|
||||
|
||||
lblLogin.AutoSize = true;
|
||||
lblLogin.Location = new Point(30, 70);
|
||||
lblLogin.Text = "Логин:";
|
||||
|
||||
txtLogin.Location = new Point(30, 90);
|
||||
txtLogin.Size = new Size(280, 23);
|
||||
|
||||
lblPassword.AutoSize = true;
|
||||
lblPassword.Location = new Point(30, 125);
|
||||
lblPassword.Text = "Пароль:";
|
||||
|
||||
txtPassword.Location = new Point(30, 145);
|
||||
txtPassword.Size = new Size(280, 23);
|
||||
|
||||
chkShowPassword.AutoSize = true;
|
||||
chkShowPassword.Location = new Point(30, 175);
|
||||
chkShowPassword.Text = "Показать пароль";
|
||||
|
||||
lblCaptcha.AutoSize = true;
|
||||
lblCaptcha.Location = new Point(30, 210);
|
||||
lblCaptcha.Text = "Введите символы с картинки:";
|
||||
|
||||
picCaptcha.BorderStyle = BorderStyle.FixedSingle;
|
||||
picCaptcha.Location = new Point(30, 230);
|
||||
picCaptcha.Size = new Size(120, 40);
|
||||
|
||||
txtCaptcha.Location = new Point(160, 240);
|
||||
txtCaptcha.Size = new Size(80, 23);
|
||||
|
||||
btnRefreshCaptcha.Location = new Point(250, 238);
|
||||
btnRefreshCaptcha.Size = new Size(60, 25);
|
||||
btnRefreshCaptcha.Text = "↻";
|
||||
|
||||
btnLogin.Location = new Point(30, 285);
|
||||
btnLogin.Size = new Size(280, 35);
|
||||
btnLogin.Text = "Войти";
|
||||
|
||||
lblError.AutoSize = false;
|
||||
lblError.ForeColor = Color.DarkRed;
|
||||
lblError.Location = new Point(30, 325);
|
||||
lblError.Size = new Size(280, 40);
|
||||
lblError.TextAlign = ContentAlignment.MiddleCenter;
|
||||
|
||||
ClientSize = new Size(350, 380);
|
||||
Controls.AddRange(new Control[] {
|
||||
lblTitle, lblLogin, txtLogin, lblPassword, txtPassword,
|
||||
chkShowPassword, lblCaptcha, picCaptcha, txtCaptcha,
|
||||
btnRefreshCaptcha, btnLogin, lblError
|
||||
});
|
||||
FormBorderStyle = FormBorderStyle.FixedSingle;
|
||||
MaximizeBox = false;
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
Text = "Вход";
|
||||
}
|
||||
|
||||
private void BtnLogin_Click(object? sender, EventArgs e)
|
||||
{
|
||||
lblError.Text = "";
|
||||
|
||||
if (LockHelper.IsLocked())
|
||||
{
|
||||
lblError.Text = "Вход заблокирован. Попробуйте позже.";
|
||||
return;
|
||||
}
|
||||
if (_captchaLockUntil.HasValue && DateTime.Now < _captchaLockUntil)
|
||||
{
|
||||
lblError.Text = $"Подождите {(_captchaLockUntil.Value - DateTime.Now).Seconds} сек.";
|
||||
return;
|
||||
}
|
||||
|
||||
string login = txtLogin.Text.Trim();
|
||||
string password = txtPassword.Text;
|
||||
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
|
||||
{
|
||||
lblError.Text = "Введите логин и пароль.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (_captchaRequired && txtCaptcha.Text.Trim() != _currentCaptcha)
|
||||
{
|
||||
lblError.Text = "Неверная captcha. Доступ заблокирован на 10 сек.";
|
||||
AppData.LogLogin(login, false);
|
||||
_captchaLockUntil = DateTime.Now.AddSeconds(FAIL_LOCK_SECONDS);
|
||||
_attemptTimer.Start();
|
||||
UpdateLockUI();
|
||||
return;
|
||||
}
|
||||
|
||||
var user = AppData.Authenticate(login, password);
|
||||
if (user == null)
|
||||
{
|
||||
_failedAttempts++;
|
||||
AppData.LogLogin(login, false);
|
||||
|
||||
if (_failedAttempts >= 2)
|
||||
{
|
||||
_captchaRequired = true;
|
||||
SetCaptchaVisible(true);
|
||||
GenerateCaptcha();
|
||||
lblError.Text = "Неудачная авторизация. Введите captcha.";
|
||||
}
|
||||
else lblError.Text = "Неверный логин или пароль.";
|
||||
return;
|
||||
}
|
||||
|
||||
AppData.LogLogin(login, true);
|
||||
var main = new MainForm(user);
|
||||
Hide();
|
||||
main.ShowDialog();
|
||||
Close();
|
||||
}
|
||||
|
||||
private void SetCaptchaVisible(bool visible)
|
||||
{
|
||||
picCaptcha.Visible = visible;
|
||||
txtCaptcha.Visible = visible;
|
||||
btnRefreshCaptcha.Visible = visible;
|
||||
lblCaptcha.Visible = visible;
|
||||
}
|
||||
|
||||
private void GenerateCaptcha()
|
||||
{
|
||||
const string chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||
var rnd = new Random();
|
||||
char[] arr = new char[3];
|
||||
for (int i = 0; i < arr.Length; i++) arr[i] = chars[rnd.Next(chars.Length)];
|
||||
_currentCaptcha = new string(arr);
|
||||
|
||||
var bmp = new Bitmap(picCaptcha.Width, picCaptcha.Height);
|
||||
using (var g = Graphics.FromImage(bmp))
|
||||
{
|
||||
g.Clear(Color.White);
|
||||
g.SmoothingMode = SmoothingMode.AntiAlias;
|
||||
var font = new Font("Arial", 24, FontStyle.Bold);
|
||||
var brush = new SolidBrush(Color.Black);
|
||||
|
||||
for (int i = 0; i < arr.Length; i++)
|
||||
{
|
||||
var state = g.Save();
|
||||
g.TranslateTransform(8 + i * 35, 20);
|
||||
g.RotateTransform(rnd.Next(-25, 25));
|
||||
g.DrawString(arr[i].ToString(), font, brush, 0, 0);
|
||||
g.Restore(state);
|
||||
}
|
||||
var pen = new Pen(Color.Gray, 1);
|
||||
for (int i = 0; i < 6; i++)
|
||||
g.DrawLine(pen, rnd.Next(bmp.Width), rnd.Next(bmp.Height),
|
||||
rnd.Next(bmp.Width), rnd.Next(bmp.Height));
|
||||
g.DrawLine(new Pen(Color.DarkRed, 1.5f), 2, bmp.Height - 5, bmp.Width - 2, 5);
|
||||
g.DrawLine(new Pen(Color.DarkBlue, 1f), 2, 5, bmp.Width - 2, bmp.Height - 5);
|
||||
for (int i = 0; i < 30; i++)
|
||||
bmp.SetPixel(rnd.Next(bmp.Width), rnd.Next(bmp.Height), Color.LightGray);
|
||||
}
|
||||
picCaptcha.Image?.Dispose();
|
||||
picCaptcha.Image = bmp;
|
||||
txtCaptcha.Clear();
|
||||
}
|
||||
|
||||
private void AttemptTimer_Tick(object? sender, EventArgs e)
|
||||
{
|
||||
if (_captchaLockUntil.HasValue && DateTime.Now >= _captchaLockUntil)
|
||||
{
|
||||
_captchaLockUntil = null;
|
||||
_attemptTimer.Stop();
|
||||
UpdateLockUI();
|
||||
lblError.Text = "Блокировка снята. Повторите ввод.";
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateLockUI()
|
||||
{
|
||||
bool locked = _captchaLockUntil.HasValue && DateTime.Now < _captchaLockUntil;
|
||||
btnLogin.Enabled = !locked && !LockHelper.IsLocked();
|
||||
txtLogin.Enabled = !locked && !LockHelper.IsLocked();
|
||||
txtPassword.Enabled = !locked && !LockHelper.IsLocked();
|
||||
txtCaptcha.Enabled = !locked;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user