Gorcupt

Untitled

Jan 31st, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Npgsql;
  11. using System.Security.Cryptography;
  12.  
  13. namespace WindowsFormsApp5
  14.  
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         NpgsqlConnectionStringBuilder sb;
  19.         private string salt = "fsdfgergerg455";
  20.        
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.             sb = new NpgsqlConnectionStringBuilder();
  25.             sb.Database = "test5";
  26.             sb.Host = "localhost";
  27.             sb.Username = "postgres";
  28.             sb.Password = "2411";
  29.         }
  30.        
  31.        
  32.  
  33.         private void txtPassword_TextChanged(object sender, EventArgs e)
  34.         {
  35.          
  36.         }
  37.  
  38.         private void txtLogin_TextChanged(object sender, EventArgs e)
  39.         {
  40.      
  41.         }
  42.  
  43.         private void button1_Click(object sender, EventArgs e)
  44.         {
  45.             using (NpgsqlConnection con = new NpgsqlConnection(sb.ToString()))
  46.             {
  47.                 if (txtLogin.Text.Trim() != "" && txtPassword.Text.Trim() != "")
  48.                 {
  49.                    
  50.                    
  51.                    
  52.                     con.Open();
  53.                     var tCommand = new NpgsqlCommand
  54.                     {
  55.                         Connection = con,
  56.                         CommandText =
  57.                             @"SELECT COUNT(*) from users where users.login = @login"
  58.                     };
  59.                     tCommand.Parameters.AddWithValue("@login", txtLogin.Text);
  60.  
  61.                     if (int.Parse(tCommand.ExecuteScalar().ToString()) == 0)
  62.                     {
  63.                         var sCommand = new NpgsqlCommand
  64.                         {
  65.                             Connection = con,
  66.                             CommandText =
  67.                                 @"INSERT INTO users(Login, Password, RegDate) VALUES (@login,@password,@Regdate)"
  68.                         };
  69.                         MD5CryptoServiceProvider hashp = new MD5CryptoServiceProvider();
  70.                         sCommand.Parameters.AddWithValue("@login", txtLogin.Text);
  71.                         sCommand.Parameters.AddWithValue("@password", GetHash(txtPassword.Text + salt));
  72.                         sCommand.Parameters.AddWithValue("@RegDate", DateTime.Now.Date);
  73.                         MessageBox.Show(
  74.                             "Регистрация прошла успешно!",
  75.                             "Сообщение");
  76.                         sCommand.ExecuteNonQuery();
  77.                     }
  78.                     else
  79.                     {
  80.                         MessageBox.Show(
  81.                             "Уже существует пользователь с данным логином!",
  82.                             "Сообщение");
  83.                     }
  84.                 }
  85.                 else
  86.                 {
  87.                     MessageBox.Show(
  88.                         "Недопускаются пустые поля!",
  89.                         "Сообщение");
  90.                 }
  91.             }
  92.         }
  93.        
  94.        
  95.         public string GetHash(string input)
  96.         {
  97.             var md5 = MD5.Create();
  98.             var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
  99.  
  100.             return Convert.ToBase64String(hash);
  101.         }
  102.  
  103.         private void button2_Click(object sender, EventArgs e)
  104.         {
  105.             using (NpgsqlConnection con = new NpgsqlConnection(sb.ToString()))
  106.             {
  107.                 con.Open();
  108.                 var kCommand = new NpgsqlCommand
  109.                 {
  110.                     Connection = con,
  111.                     CommandText =
  112.                         @"SELECT COUNT(*) from users where users.password = @password and users.login = @login1"
  113.                 };
  114.                 kCommand.Parameters.AddWithValue("@password", GetHash(txtPassword.Text+salt));
  115.                 kCommand.Parameters.AddWithValue("@login1", txtLogin.Text);
  116.                 if (int.Parse(kCommand.ExecuteScalar().ToString()) == 1)
  117.                 {
  118.                     MessageBox.Show(
  119.                         "Вы вошли!",
  120.                         "Сообщение");
  121.                 }
  122.                 else
  123.                 {
  124.                     MessageBox.Show(
  125.                         "Неверный логин или пароль!",
  126.                         "Сообщение");
  127.                 }
  128.             }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment