Diplom_O/MainForms/LoginForm.cs
2024-09-20 06:53:52 +03:00

65 lines
1.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 Diplom_O.DataBase;
using System.Security.Cryptography;
namespace Diplom_O
{
public partial class LoginForm : Form
{
public bool isCanceled = false;
public LoginForm()
{
InitializeComponent();
try
{
loginBox.Items.Clear();
var userList = FuncDB.UserList();
foreach (var usr in userList)
loginBox.Items.Add(usr.Login);
if (loginBox.Items.Count > 0)
loginBox.SelectedIndex = 0;
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
workButton.Enabled = false;
}
}
private void workButton_Click(object sender, EventArgs e)
{
try
{
var _user = FuncDB.UserGetByLogin((string)loginBox.Text);
if (_user == null)
throw new Exception("Пользователя не существует.");
if (_user.Pass != textBox1.Text)
throw new Exception("Не верный пароль.");
byte[] pass_bytes = Encoding.ASCII.GetBytes(textBox1.Text);
var pass_hash_bytes = new MD5CryptoServiceProvider().ComputeHash(pass_bytes);
_user.Pass = Encoding.ASCII.GetString(pass_hash_bytes);
this.SetUser(_user);
this.SetNextForm(_user.Default);
this.Close();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void canceledButton_Click(object sender, EventArgs e)
{
isCanceled = true;
this.Close();
}
}
}