Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Npgsql;
- using System.Security.Cryptography;
- namespace WindowsFormsApp5
- {
- public partial class Form1 : Form
- {
- NpgsqlConnectionStringBuilder sb;
- private string salt = "fsdfgergerg455";
- public Form1()
- {
- InitializeComponent();
- sb = new NpgsqlConnectionStringBuilder();
- sb.Database = "test5";
- sb.Host = "localhost";
- sb.Username = "postgres";
- sb.Password = "2411";
- }
- private void txtPassword_TextChanged(object sender, EventArgs e)
- {
- }
- private void txtLogin_TextChanged(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- using (NpgsqlConnection con = new NpgsqlConnection(sb.ToString()))
- {
- if (txtLogin.Text.Trim() != "" && txtPassword.Text.Trim() != "")
- {
- con.Open();
- var tCommand = new NpgsqlCommand
- {
- Connection = con,
- CommandText =
- @"SELECT COUNT(*) from users where users.login = @login"
- };
- tCommand.Parameters.AddWithValue("@login", txtLogin.Text);
- if (int.Parse(tCommand.ExecuteScalar().ToString()) == 0)
- {
- var sCommand = new NpgsqlCommand
- {
- Connection = con,
- CommandText =
- @"INSERT INTO users(Login, Password, RegDate) VALUES (@login,@password,@Regdate)"
- };
- MD5CryptoServiceProvider hashp = new MD5CryptoServiceProvider();
- sCommand.Parameters.AddWithValue("@login", txtLogin.Text);
- sCommand.Parameters.AddWithValue("@password", GetHash(txtPassword.Text + salt));
- sCommand.Parameters.AddWithValue("@RegDate", DateTime.Now.Date);
- MessageBox.Show(
- "Регистрация прошла успешно!",
- "Сообщение");
- sCommand.ExecuteNonQuery();
- }
- else
- {
- MessageBox.Show(
- "Уже существует пользователь с данным логином!",
- "Сообщение");
- }
- }
- else
- {
- MessageBox.Show(
- "Недопускаются пустые поля!",
- "Сообщение");
- }
- }
- }
- public string GetHash(string input)
- {
- var md5 = MD5.Create();
- var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
- return Convert.ToBase64String(hash);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- using (NpgsqlConnection con = new NpgsqlConnection(sb.ToString()))
- {
- con.Open();
- var kCommand = new NpgsqlCommand
- {
- Connection = con,
- CommandText =
- @"SELECT COUNT(*) from users where users.password = @password and users.login = @login1"
- };
- kCommand.Parameters.AddWithValue("@password", GetHash(txtPassword.Text+salt));
- kCommand.Parameters.AddWithValue("@login1", txtLogin.Text);
- if (int.Parse(kCommand.ExecuteScalar().ToString()) == 1)
- {
- MessageBox.Show(
- "Вы вошли!",
- "Сообщение");
- }
- else
- {
- MessageBox.Show(
- "Неверный логин или пароль!",
- "Сообщение");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment