FormAuthorization.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace СУБД_Швейная_фабрика
  12. {
  13. public partial class FormAuthorization : Form
  14. {
  15. public FormAuthorization()
  16. {
  17. InitializeComponent();
  18. }
  19. public static string StrCon = @"Data Source=213.155.192.79,3002;Initial Catalog=FabrikaLDA;User ID=u21liseenko;Password=bfg2";
  20. private void cbxPassword_CheckedChanged(object sender, EventArgs e)
  21. {
  22. tbxPassword.UseSystemPasswordChar = !tbxPassword.UseSystemPasswordChar;
  23. }
  24. public static string IdUser = "";
  25. private void lblReg_Click(object sender, EventArgs e)
  26. {
  27. FormRegistration frm = new FormRegistration();
  28. this.Hide();
  29. frm.ShowDialog();
  30. this.Show();
  31. }
  32. void GetCaptcha()
  33. {
  34. string TxtLetter = "QWERTYUIOPASDFGHJKLZXCVBNM";
  35. string TxtDigit = "1234567890";
  36. string captcha = "";
  37. string tmp = TxtDigit + TxtLetter;
  38. Random rnd = new Random();
  39. bool fDigit = false;
  40. for (int i = 0;i <3;i++)
  41. {
  42. char sym = tmp[rnd.Next(tmp.Length)];
  43. if (char.IsDigit(sym))
  44. {
  45. fDigit = true;
  46. }
  47. captcha += sym;
  48. }
  49. if (fDigit == false)
  50. captcha += TxtDigit[rnd.Next(TxtDigit.Length)];
  51. else
  52. captcha += TxtLetter[rnd.Next(TxtLetter.Length)];
  53. lblCaptcha.Text = captcha;
  54. }
  55. private void btnLogin_Click(object sender, EventArgs e)
  56. {
  57. if (lblCaptcha.Text != tbxCaptcha.Text.ToUpper())
  58. {
  59. MessageBox.Show("Повторите ввод капчи!","Внимание!",MessageBoxButtons.OK, MessageBoxIcon.Error);
  60. GetCaptcha();
  61. return;
  62. }
  63. SqlConnection con = new SqlConnection(StrCon);
  64. SqlCommand cmd = new SqlCommand($"select IdUser,login,Password,Role from Users where Login = '{tbxLogin.Text.Trim()}' and Password = '{tbxPassword.Text.Trim()}'", con);
  65. con.Open();
  66. SqlDataReader res = cmd.ExecuteReader();
  67. res.Read();
  68. if (res.HasRows)
  69. {
  70. if (res["Role"].ToString() == "1")
  71. {
  72. FormManager frm = new FormManager();
  73. this.Hide();
  74. IdUser = res["IdUser"].ToString();
  75. tbxLogin.Text = "";
  76. tbxPassword.Text = "";
  77. frm.ShowDialog();
  78. this.Show();
  79. }
  80. if (res["Role"].ToString() == "2")
  81. {
  82. FormStorekeeper frm = new FormStorekeeper();
  83. this.Hide();
  84. IdUser = res["IdUser"].ToString();
  85. tbxLogin.Text = "";
  86. tbxPassword.Text = "";
  87. frm.ShowDialog();
  88. this.Show();
  89. }
  90. if (res["Role"].ToString() == "3")
  91. {
  92. FormClient frm = new FormClient();
  93. this.Hide();
  94. IdUser = res["IdUser"].ToString();
  95. tbxLogin.Text = "";
  96. tbxPassword.Text = "";
  97. frm.ShowDialog();
  98. this.Show();
  99. }
  100. }
  101. else
  102. {
  103. MessageBox.Show("Ошибка авторизации!","Логин или пароль неправильны",MessageBoxButtons.OK, MessageBoxIcon.Error);
  104. GetCaptcha();
  105. return;
  106. }
  107. con.Close();
  108. }
  109. private void FormAuthorization_Load(object sender, EventArgs e)
  110. {
  111. GetCaptcha();
  112. //14 dawdawd
  113. tbxCaptcha.Text = lblCaptcha.Text;
  114. //
  115. }
  116. private void pCap_Paint(object sender, PaintEventArgs e)
  117. {
  118. Color[] colors = { Color.Green, Color.Black, Color.Yellow, Color.White };
  119. Random rnd = new Random();
  120. for (int i = 0; i < rnd.Next(5, 10); i++)
  121. {
  122. int x1 = rnd.Next(pCap.Width);
  123. int x2 = rnd.Next(pCap.Width);
  124. int y1 = rnd.Next(pCap.Height);
  125. int y2 = rnd.Next(pCap.Height);
  126. Color color = colors[rnd.Next(colors.Length)];
  127. e.Graphics.DrawLine(new Pen(color), x1, y1, x2, y2);
  128. }
  129. }
  130. }
  131. }