using ApplicationAvalonia.Models; using ApplicationAvalonia.Utilities; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.EntityFrameworkCore; using MsBox.Avalonia; using MsBox.Avalonia.Enums; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace ApplicationAvalonia.ViewModels { internal partial class RegestViewModel : ViewModelBase { PageSwitcher pgSwitch; #region Properties [ObservableProperty] private string _login = string.Empty; [ObservableProperty] private string _password = string.Empty; [ObservableProperty] private string _name = string.Empty; [ObservableProperty] private string _surname = string.Empty; [ObservableProperty] private string _message = string.Empty; #endregion public RegestViewModel(ref PageSwitcher pageSwitcher) { this.pgSwitch = pageSwitcher; } [RelayCommand] private async void RegistrationBtn() { Message = ""; // AES encrypted if (Login.Trim() != "" && Password.Trim() != "" && Name.Trim() != "" && Surname.Trim() != "") { byte[] key = new byte[16]; byte[] iv = new byte[16]; using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) { rng.GetBytes(key); rng.GetBytes(iv); } byte[] encryptedPassword = EncryptionClass.Encrypt(Password, key, iv); LoginTable newUser = new LoginTable() { Login = Login.Trim(), Password = encryptedPassword, Iv = iv, Key = key }; dbContext.LoginTables.Add(newUser); dbContext.SaveChanges(); var userInDB = dbContext.LoginTables.SingleOrDefault(x => encryptedPassword == x.Password); if (userInDB != null) { UserTable userTable = new UserTable() { IdLoginNavigation = userInDB, IdLogin = userInDB.Id, UserName = Name.Trim(), UserSurname = Surname.Trim(), IdRole = 2 }; dbContext.UserTables.Add(userTable); dbContext.SaveChanges(); var mbox = MessageBoxManager.GetMessageBoxStandard("Уведомление", "Регистрация прошла успешно перенаправляем вас на другую страницу", ButtonEnum.Ok, Icon.Success); var waiter = await mbox.ShowWindowAsync(); pgSwitch.View = new AuthViewModel(ref pgSwitch); return; } Message = "Что-то пошло не так"; } else { Message = "Поля или поле не заполнены"; } } } }