Dmismuth

Form1

Dec 3rd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 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 System.Data.SQLite;
  11. using System.Security.Cryptography;
  12.  
  13. namespace Sale_on_credit
  14. {
  15. public partial class Form1 : MetroFramework.Forms.MetroForm
  16. {
  17. Boolean sym;
  18. string loginDB;
  19. string passwordDB;
  20. string login;
  21. string password;
  22. string hash_login;
  23. string hash_password;
  24. SQLiteCommand cmd;
  25. SQLiteConnection sqlite_conn;
  26. SQLiteDataReader reader;
  27. static int errors=0;
  28.  
  29. public Form1()
  30. {
  31. InitializeComponent();
  32. this.StyleManager = metroStyleManager1;
  33. this.FocusMe();
  34. sqlite_conn = new SQLiteConnection(getConnectionString());
  35. sqlite_conn.Open();
  36. }
  37.  
  38. //Метод для получения адреса к БД
  39. public string getConnectionString()
  40. {
  41. string connectionString = @"Data Source=BD_Sales.db;Version=3";
  42. return connectionString;
  43. }
  44.  
  45. public string MD5(string input)
  46. {
  47. MD5 md5 = new MD5CryptoServiceProvider();
  48. byte[] bytes = md5.ComputeHash(Encoding.Unicode.GetBytes(input));
  49. string result = BitConverter.ToString(bytes).Replace("-", String.Empty);
  50. return result.ToLower();
  51. }
  52.  
  53. public string Authorization(string hash_login, string hash_password)
  54. {
  55. if (textBox1.Text == "") return "Введите логин пользователя";
  56. if (textBox2.Text == "") return "Введите пароль пользователя";
  57. string connectionString = getConnectionString();
  58.  
  59. if (textBox1.Text == "admin")
  60. {
  61.  
  62.  
  63. cmd = new SQLiteCommand("SELECT * FROM admin WHERE id = 1", sqlite_conn);
  64.  
  65. reader = cmd.ExecuteReader();
  66. while (reader.Read())
  67. {
  68. if (reader["login"].ToString() == hash_login)
  69. {
  70. if (reader["password"].ToString() == hash_password)
  71. {
  72. reader.Close();
  73. sqlite_conn.Close();
  74. return "*";
  75. }
  76. else return "Введен неверный пароль!!!";
  77. }
  78. else return "Такого логина не существует!!!";
  79. }
  80. reader.Close();
  81.  
  82. // sqlite_conn.Close();
  83. }
  84.  
  85. cmd = new SQLiteCommand("SELECT * FROM users WHERE login ='" + hash_login + "'", sqlite_conn);
  86. reader = cmd.ExecuteReader();
  87. while (reader.Read())
  88. {
  89. if (reader["login"].ToString() == hash_login)
  90. {
  91. if (reader["password"].ToString() == hash_password)
  92. {
  93. sqlite_conn.Close();
  94. return "*";
  95. }
  96. else return "Введен неверный пароль!!!";
  97. }
  98. else return "Такого логина не существует!!!";
  99. }
  100. reader.Close();
  101. return "Введены неверные данные!!!";
  102.  
  103. }
  104.  
  105. private void button1_Click(object sender, EventArgs e)
  106. {
  107. string connectionString = getConnectionString();
  108. SQLiteCommand sqlite_cmd;
  109.  
  110. login = textBox1.Text;
  111. password = textBox2.Text;
  112. hash_login = MD5(textBox1.Text);
  113. hash_password = MD5(textBox2.Text);
  114.  
  115.  
  116.  
  117. string s = Authorization(hash_login,hash_password);
  118. if (textBox1.Text == "admin" && s == "*")
  119. {
  120. Form2 form2 = new Form2();
  121. form2.Visible = true;
  122. Visible = false;
  123.  
  124. }
  125. else if (textBox1.Text == "admin" && s != "*")
  126. {
  127. MessageBox.Show("Ошибка!!!Проверьте правильность введенных данных!!!");
  128. }else if (textBox1.Text != "admin" && s == "*")
  129. {
  130. Form3 form3 = new Form3();
  131. form3.Visible = true;
  132. Visible = false;
  133. }
  134. else
  135. {
  136. MessageBox.Show("Ошибка!!!Проверьте правильность введенных данных!!!");
  137. }
  138.  
  139.  
  140.  
  141. }
  142.  
  143. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  144. {
  145. Application.Exit();
  146.  
  147. }
  148. }
  149. }
Add Comment
Please, Sign In to add comment