Esempio n. 1
0
 private Task <User> GetUserFromLogin(DataAccess.AppContext context, string login)
 {
     return(Task.Run(() =>
     {
         return context.Users.SingleOrDefault(seatchingUser => seatchingUser.Login == login);
     }));
 }
Esempio n. 2
0
 private Task CreateUser(DataAccess.AppContext context, User user)
 {
     return(Task.Run(() =>
     {
         context.Users.Add(user);
     }));
 }
        private async void SignInButtonClick(object sender, RoutedEventArgs e)
        {
            var login = loginTextBox.Text;

            var password = passwordBox.Password;

            if (string.IsNullOrWhiteSpace(login) || string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("Заполните все поля!");
                return;
            }

            signInButton.IsEnabled       = false;
            registrationButton.IsEnabled = false;

            using (var context = new DataAccess.AppContext())
            {
                var user = await GetUserFromLogin(context, login);

                if (user == null || !SecurityHasher.VerifyPassword(password, user.Password))
                {
                    MessageBox.Show("Неверный логин или пароль");
                }
                else
                {
                    new CabinetWindow().Show();
                    Close();
                }
            }

            signInButton.IsEnabled       = true;
            registrationButton.IsEnabled = true;
        }
Esempio n. 4
0
        private async void RegistrationButtonClick(object sender, RoutedEventArgs e)
        {
            string login = loginTextBox.Text;

            string password = passwordTextBox.Password;

            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("Заполните все поля!");
                return;
            }

            registrationButton.IsEnabled = false;

            using (var context = new DataAccess.AppContext())
            {
                var user = await GetUserFromLogin(context, login);

                if (user == null)
                {
                    await CreateUser(context, new User
                    {
                        Login    = login,
                        Password = SecurityHasher.HashPassword(password)
                    });

                    await context.SaveChangesAsync();

                    MessageBox.Show("Учетная запись успешно создана!");
                    Close();
                }
                else
                {
                    MessageBox.Show("Логин уже занят!");
                }
            }

            registrationButton.IsEnabled = true;
        }