| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Windows.Forms;
- using Npgsql;
- namespace PhotoStudioApp
- {
- public partial class LoginForm : Form
- {
- private string connectionString = "Host=245e1-rw.db.pub.dbaas.postgrespro.ru;Port=5432;Database=dbproject;Username=smirnova_ul;Password=Q#Rto%g$$$5;SSL Mode=Require;Trust Server Certificate=true;";
- public LoginForm()
- {
- InitializeComponent();
- }
- private void btnLogin_Click(object sender, EventArgs e)
- {
- string login = txtLogin.Text;
- string password = txtPassword.Text;
- string hash = GetSHA256Hash(password);
- using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
- {
- conn.Open();
- string sql = "select u.user_id, r.role_name from users u join roles r on u.role_id = r.role_id where u.login = @login and u.password_hash = @hash";
- NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
- cmd.Parameters.AddWithValue("@login", login);
- cmd.Parameters.AddWithValue("@hash", hash);
- using (NpgsqlDataReader reader = cmd.ExecuteReader())
- {
- if (reader.Read())
- {
- int userId = reader.GetInt32(0);
- string role = reader.GetString(1);
- MessageBox.Show($"Добро пожаловать, {login}! Роль: {role}");
- MainForm mainForm = new MainForm(userId, role, login, connectionString);
- mainForm.Show();
- this.Hide();
- }
- else
- {
- MessageBox.Show("Неверный логин или пароль");
- }
- }
- }
- }
- private string GetSHA256Hash(string input)
- {
- using (System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create())
- {
- byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
- byte[] hashBytes = sha256.ComputeHash(inputBytes);
- return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
- }
- }
- protected override void OnFormClosing(FormClosingEventArgs e)
- {
- if (this.Owner == null)
- {
- Application.Exit();
- }
- base.OnFormClosing(e);
- }
- }
- }
|