| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using Npgsql;
- using System;
- using System.Security.Cryptography;
- using System.Text;
- using System.Windows.Forms;
- namespace DeanOfficeApp
- {
- public partial class LoginForm : Form
- {
- private readonly string _connString = "Host=245e1-rw.db.pub.dbaas.postgrespro.ru;Port=5432;Database=dbpractice;Username=mardasov_mv;Password=A4%2x$$2Ynr";
- public string UserRole { get; private set; }
- public string Username { get; private set; }
- public string FullName { get; private set; }
- public LoginForm()
- {
- InitializeComponent();
- txtPassword.PasswordChar = '*';
- }
- private async void btnLogin_Click(object sender, EventArgs e)
- {
- string username = txtUsername.Text.Trim();
- string password = txtPassword.Text;
- if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
- {
- MessageBox.Show("Введите логин и пароль!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- string hash = ComputeMd5(password);
- try
- {
- using (var conn = new NpgsqlConnection(_connString))
- {
- await conn.OpenAsync();
- string sql = "SELECT role, full_name FROM appuser WHERE username = @user AND password_hash = @hash";
- using (var cmd = new NpgsqlCommand(sql, conn))
- {
- cmd.Parameters.AddWithValue("@user", username);
- cmd.Parameters.AddWithValue("@hash", hash);
- using (var reader = await cmd.ExecuteReaderAsync())
- {
- if (await reader.ReadAsync())
- {
- UserRole = reader.GetString(0);
- FullName = reader.GetString(1);
- Username = username;
- DialogResult = DialogResult.OK;
- Close();
- }
- else
- {
- MessageBox.Show("Неверный логин или пароль!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"Ошибка подключения: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private string ComputeMd5(string input)
- {
- using (MD5 md5 = MD5.Create())
- {
- byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
- StringBuilder sb = new StringBuilder();
- foreach (byte b in hash) sb.Append(b.ToString("x2"));
- return sb.ToString();
- }
- }
- private void btnExit_Click(object sender, EventArgs e) => Application.Exit();
- private void chkShowPassword_CheckedChanged(object sender, EventArgs e) => txtPassword.PasswordChar = chkShowPassword.Checked ? '\0' : '*';
- private void panelLogin_Paint(object sender, PaintEventArgs e)
- {
- }
- }
- }
|