Advertisement
Guest User

registration postgresql

a guest
Jan 25th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Npgsql;
  12.  
  13. namespace Учебная_практика_БД
  14. {
  15.     public partial class RegistrationForm : Form
  16.     {
  17.         private readonly Regex loginRegex = new Regex("[А-Яа-я0-9]{6,50}$");
  18.  
  19.         private readonly string sConnStr = new NpgsqlConnectionStringBuilder
  20.         {
  21.             Host = "localhost",
  22.             Port = 5432,
  23.             Database = "user_registration",
  24.             Username = "pg_student",
  25.             Password = "pg_student"
  26.         }.ConnectionString;
  27.  
  28.         private static byte[] CalcHash(string filePath)
  29.         {
  30.             return Org.BouncyCastle.Security.DigestUtilities.CalculateDigest("GOST3411-2012-256",
  31.                 System.IO.File.ReadAllBytes(filePath)
  32.                 );
  33.         }
  34.  
  35.  
  36.         public RegistrationForm()
  37.         {
  38.             InitializeComponent();
  39.         }
  40.  
  41.         private void BtLoad_Click(object sender, EventArgs e)
  42.         {
  43.             if(ofdImage.ShowDialog() == DialogResult.OK)
  44.             {
  45.                 password.ImageLocation = ofdImage.FileName;
  46.             }
  47.         }
  48.  
  49.         private void TbLogin_TextChanged(object sender, EventArgs e)
  50.         {
  51.             if (!loginRegex.IsMatch(tbLogin.Text))
  52.             {
  53.                 epMain.SetError(tbLogin, "Логин должен быть длиной от 6 до 50 символов и содержать только русские буквы и цифры!");
  54.                 btReg.Enabled = false;
  55.                 return;
  56.             }
  57.  
  58.             using (var sConn = new NpgsqlConnection(sConnStr))
  59.             {
  60.                 sConn.Open();
  61.                 var sCommand = new NpgsqlCommand
  62.                 {
  63.                     Connection = sConn,
  64.                     CommandText = @"
  65.                         SELECT COUNT(*)
  66.                         FROM users
  67.                         WHERE lower(login) = lower(@currentLogin)"
  68.                 };
  69.                 sCommand.Parameters.AddWithValue("@currentLogin", tbLogin.Text);
  70.                 if((long) sCommand.ExecuteScalar() > 0)
  71.                 {
  72.                     epMain.SetError(tbLogin, "Логин уже занят");
  73.                     return;
  74.                 }
  75.             }
  76.             btReg.Enabled = true;
  77.             epMain.SetError(tbLogin, "");
  78.         }
  79.  
  80.         private void BtReg_Click(object sender, EventArgs e)
  81.         {
  82.             using (var sConn = new NpgsqlConnection(sConnStr))
  83.             {
  84.                 sConn.Open();
  85.                 var sCommand = new NpgsqlCommand
  86.                 {
  87.                     Connection = sConn,
  88.                     CommandText = @"INSERT INTO users(login, image_hash) VALUES(@currentLogin, @imageHash)"
  89.                 };
  90.                 sCommand.Parameters.AddWithValue("@currentLogin", tbLogin.Text);
  91.                 sCommand.Parameters.AddWithValue("@imageHash",
  92.                     CalcHash(password.ImageLocation));
  93.                 sCommand.ExecuteNonQuery();
  94.             }
  95.            
  96.         }
  97.     }
  98. }
  99.  
  100. /*
  101.  * Реализовать приложение, которое позволяет регистрировать пользователя. Сделать пароль текстовым.
  102.  * изменить хеш функцию так, чтобы одинковые пароли не давали одинаковые хеш функции, используя соль
  103.  * реализовать аутентификацию. Проверять пароль на сложность. В логине можно ставить пробелы.
  104.  *
  105.  *
  106.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement