LoginForm.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. namespace AutorentWinForms;
  2. public sealed class LoginForm : Form
  3. {
  4. private readonly Database _db;
  5. private readonly TextBox _loginBox = new() { Text = "garanin_gi", Width = 220 };
  6. private readonly TextBox _passwordBox = new() { UseSystemPasswordChar = true, Width = 220 };
  7. private readonly Label _message = new() { AutoSize = true, ForeColor = Color.Firebrick };
  8. public UserSession? Session { get; private set; }
  9. public LoginForm(Database db)
  10. {
  11. _db = db;
  12. Text = "Автопрокат - вход";
  13. StartPosition = FormStartPosition.CenterScreen;
  14. FormBorderStyle = FormBorderStyle.FixedDialog;
  15. MaximizeBox = false;
  16. MinimizeBox = false;
  17. ClientSize = new Size(420, 230);
  18. Font = new Font("Segoe UI", 10F);
  19. var title = new Label
  20. {
  21. Text = "Информационная система «Автопрокат»",
  22. Font = new Font(Font, FontStyle.Bold),
  23. AutoSize = true,
  24. Location = new Point(32, 26)
  25. };
  26. var hint = new Label
  27. {
  28. Text = "Вход выполняется по учетной записи PostgreSQL.",
  29. ForeColor = Color.DimGray,
  30. AutoSize = false,
  31. Size = new Size(350, 24),
  32. Location = new Point(32, 58)
  33. };
  34. var loginLabel = new Label { Text = "Логин", AutoSize = true, Location = new Point(32, 104) };
  35. _loginBox.Location = new Point(150, 100);
  36. var passwordLabel = new Label { Text = "Пароль", AutoSize = true, Location = new Point(32, 144) };
  37. _passwordBox.Location = new Point(150, 140);
  38. var loginButton = new Button { Text = "Войти", Width = 105, Height = 34, Location = new Point(265, 178) };
  39. loginButton.Click += (_, _) => TryLogin();
  40. AcceptButton = loginButton;
  41. _message.Location = new Point(32, 186);
  42. Controls.AddRange([title, hint, loginLabel, _loginBox, passwordLabel, _passwordBox, _message, loginButton]);
  43. }
  44. private void TryLogin()
  45. {
  46. Session = _db.Authenticate(_loginBox.Text, _passwordBox.Text);
  47. if (Session is null)
  48. {
  49. _message.Text = "Неверный логин или пароль.";
  50. return;
  51. }
  52. _db.SetSession(Session);
  53. DialogResult = DialogResult.OK;
  54. Close();
  55. }
  56. }