123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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 System.Data.SqlClient;
- namespace Швейная_фабрика
- {
- public partial class fAuthorization : Form
- {
- public static string txtCon = "Data Source=213.155.192.79,3002;Initial Catalog=SewingFactory;User ID=u22kudinov;Password=vhk5";
- SqlConnection con = new SqlConnection(txtCon);
- public static string UserID;
- public fAuthorization()
- {
- InitializeComponent();
- }
- private void bAuthorize_Click(object sender, EventArgs e)
- {
- if (tbxLogin.Text == "")
- {
- MessageBox.Show("Чтобы авторизоваться в программе, нужно ввести логин.", "Ошибка при авторизации", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- tbxLogin.Focus();
- return;
- }
- if (tbxPassword.Text == "")
- {
- MessageBox.Show("Чтобы авторизоваться в программе, нужно ввести пароль.", "Ошибка при авторизации", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- tbxPassword.Focus();
- return;
- }
- if (tbxCaptcha.Text != lblCaptcha.Text)
- {
- MessageBox.Show("Чтобы авторизоваться в программе, нужно правильно ввести символы CAPTCHA.", "Ошибка при авторизации", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- tbxCaptcha.Text = "";
- lblCaptcha.Text = GetCaptcha();
- tbxCaptcha.Focus();
- return;
- }
- con.Open();
- string txtQuery = $"select * from Users where Login = '{tbxLogin.Text}' and Password = '{tbxPassword.Text}' and RoleID = {cbxRole.SelectedIndex + 1}";
- SqlCommand query = new SqlCommand(txtQuery, con);
- SqlDataReader res = query.ExecuteReader();
- if (res.HasRows)
- {
- res.Read();
- UserID = res["UserID"].ToString();
- if (res["RoleID"].ToString() == "1")
- {
- fCustomer f = new fCustomer();
- f.Show();
- tbxLogin.Clear();
- tbxPassword.Clear();
- this.Hide();
- }
- else if (res["RoleID"].ToString() == "2")
- {
- fManager f = new fManager();
- f.Show();
- tbxLogin.Clear();
- tbxPassword.Clear();
- this.Hide();
- }
- else if (res["RoleID"].ToString() == "3")
- {
- fStorekeeper f = new fStorekeeper();
- f.Show();
- tbxLogin.Clear();
- tbxPassword.Clear();
- this.Hide();
- }
- }
- else
- {
- MessageBox.Show("Пользователя с такими типом пользователя, логином и паролем не существует.", "Ошибка при входе", MessageBoxButtons.OK, MessageBoxIcon.Information);
- con.Close();
- return;
- }
- con.Close();
- }
- void Roles()
- {
- con.Open();
- string txtQuery = "select RoleName from Roles";
- SqlCommand query = new SqlCommand(txtQuery, con);
- SqlDataReader res = query.ExecuteReader();
- while (res.Read())
- {
- cbxRole.Items.Add(res["RoleName"]);
- }
- con.Close();
- }
- string GetCaptcha()
- {
- string txtLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- string txtDigit = "0123456789";
- string t1 = txtLetter + txtDigit;
- string captcha = "";
- bool SymbolIsDigit = false;
- Random rnd = new Random();
- for (int i = 1; i <= 3; i++)
- {
- char sim = t1[rnd.Next(t1.Length)];
- if (char.IsDigit(sim)) SymbolIsDigit = true;
- captcha += sim;
- }
- if (SymbolIsDigit == false) captcha += txtDigit[rnd.Next(txtDigit.Length)];
- else captcha += txtLetter[rnd.Next(txtLetter.Length)];
- return captcha;
- }
- private void fAuthorization_Load(object sender, EventArgs e)
- {
- Roles();
- cbxRole.SelectedIndex = 1;
- lblCaptcha.Text = GetCaptcha();
- }
- private void bRegistration_Click(object sender, EventArgs e)
- {
- fRegistration f = new fRegistration();
- f.Show();
- this.Hide();
- }
- private void lblCaptcha_Paint(object sender, PaintEventArgs e)
- {
- Color[] colors = { Color.Green, Color.Black, Color.Yellow, Color.White };
- Random rnd = new Random();
- for (int i = 1; i <= rnd.Next(5, 11); i++)
- {
- int x1 = rnd.Next(lblCaptcha.Width);
- int y1 = rnd.Next(lblCaptcha.Height);
- int x2 = rnd.Next(lblCaptcha.Width);
- int y2 = rnd.Next(lblCaptcha.Height);
- Color col = colors[rnd.Next(colors.Length)];
- e.Graphics.DrawLine(new Pen(col), x1, y1, x2, y2);
- }
- }
- }
- }
|