fAuthorization.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.SqlClient;
  11. namespace Швейная_фабрика
  12. {
  13. public partial class fAuthorization : Form
  14. {
  15. public static string txtCon = "Data Source=213.155.192.79,3002;Initial Catalog=SewingFactory;User ID=u22kudinov;Password=vhk5";
  16. SqlConnection con = new SqlConnection(txtCon);
  17. public static string UserID;
  18. public fAuthorization()
  19. {
  20. InitializeComponent();
  21. }
  22. private void bAuthorize_Click(object sender, EventArgs e)
  23. {
  24. if (tbxLogin.Text == "")
  25. {
  26. MessageBox.Show("Чтобы авторизоваться в программе, нужно ввести логин.", "Ошибка при авторизации", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  27. tbxLogin.Focus();
  28. return;
  29. }
  30. if (tbxPassword.Text == "")
  31. {
  32. MessageBox.Show("Чтобы авторизоваться в программе, нужно ввести пароль.", "Ошибка при авторизации", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  33. tbxPassword.Focus();
  34. return;
  35. }
  36. if (tbxCaptcha.Text != lblCaptcha.Text)
  37. {
  38. MessageBox.Show("Чтобы авторизоваться в программе, нужно правильно ввести символы CAPTCHA.", "Ошибка при авторизации", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  39. tbxCaptcha.Text = "";
  40. lblCaptcha.Text = GetCaptcha();
  41. tbxCaptcha.Focus();
  42. return;
  43. }
  44. con.Open();
  45. string txtQuery = $"select * from Users where Login = '{tbxLogin.Text}' and Password = '{tbxPassword.Text}' and RoleID = {cbxRole.SelectedIndex + 1}";
  46. SqlCommand query = new SqlCommand(txtQuery, con);
  47. SqlDataReader res = query.ExecuteReader();
  48. if (res.HasRows)
  49. {
  50. res.Read();
  51. UserID = res["UserID"].ToString();
  52. if (res["RoleID"].ToString() == "1")
  53. {
  54. fCustomer f = new fCustomer();
  55. f.Show();
  56. tbxLogin.Clear();
  57. tbxPassword.Clear();
  58. this.Hide();
  59. }
  60. else if (res["RoleID"].ToString() == "2")
  61. {
  62. fManager f = new fManager();
  63. f.Show();
  64. tbxLogin.Clear();
  65. tbxPassword.Clear();
  66. this.Hide();
  67. }
  68. else if (res["RoleID"].ToString() == "3")
  69. {
  70. fStorekeeper f = new fStorekeeper();
  71. f.Show();
  72. tbxLogin.Clear();
  73. tbxPassword.Clear();
  74. this.Hide();
  75. }
  76. }
  77. else
  78. {
  79. MessageBox.Show("Пользователя с такими типом пользователя, логином и паролем не существует.", "Ошибка при входе", MessageBoxButtons.OK, MessageBoxIcon.Information);
  80. con.Close();
  81. return;
  82. }
  83. con.Close();
  84. }
  85. void Roles()
  86. {
  87. con.Open();
  88. string txtQuery = "select RoleName from Roles";
  89. SqlCommand query = new SqlCommand(txtQuery, con);
  90. SqlDataReader res = query.ExecuteReader();
  91. while (res.Read())
  92. {
  93. cbxRole.Items.Add(res["RoleName"]);
  94. }
  95. con.Close();
  96. }
  97. string GetCaptcha()
  98. {
  99. string txtLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  100. string txtDigit = "0123456789";
  101. string t1 = txtLetter + txtDigit;
  102. string captcha = "";
  103. bool SymbolIsDigit = false;
  104. Random rnd = new Random();
  105. for (int i = 1; i <= 3; i++)
  106. {
  107. char sim = t1[rnd.Next(t1.Length)];
  108. if (char.IsDigit(sim)) SymbolIsDigit = true;
  109. captcha += sim;
  110. }
  111. if (SymbolIsDigit == false) captcha += txtDigit[rnd.Next(txtDigit.Length)];
  112. else captcha += txtLetter[rnd.Next(txtLetter.Length)];
  113. return captcha;
  114. }
  115. private void fAuthorization_Load(object sender, EventArgs e)
  116. {
  117. Roles();
  118. cbxRole.SelectedIndex = 1;
  119. lblCaptcha.Text = GetCaptcha();
  120. }
  121. private void bRegistration_Click(object sender, EventArgs e)
  122. {
  123. fRegistration f = new fRegistration();
  124. f.Show();
  125. this.Hide();
  126. }
  127. private void lblCaptcha_Paint(object sender, PaintEventArgs e)
  128. {
  129. Color[] colors = { Color.Green, Color.Black, Color.Yellow, Color.White };
  130. Random rnd = new Random();
  131. for (int i = 1; i <= rnd.Next(5, 11); i++)
  132. {
  133. int x1 = rnd.Next(lblCaptcha.Width);
  134. int y1 = rnd.Next(lblCaptcha.Height);
  135. int x2 = rnd.Next(lblCaptcha.Width);
  136. int y2 = rnd.Next(lblCaptcha.Height);
  137. Color col = colors[rnd.Next(colors.Length)];
  138. e.Graphics.DrawLine(new Pen(col), x1, y1, x2, y2);
  139. }
  140. }
  141. }
  142. }