public async Task <UserModel> Update(UserUpdatesModel model) { var user = await currentUser.GetUser(); user.FirstName = model.FirstName; user.LastName = model.LastName; if (!String.IsNullOrEmpty(model.NewPassword)) { var encryptedPassword = passwordEncrypter.Encrypt(model.Password); if (user.Password == encryptedPassword) { user.Password = passwordEncrypter.Encrypt(model.NewPassword); } } if (model.Image != null) { user.AvatarBase64 = model.Image; } await dataContext.SaveChangesAsync(); return(await currentUser.GetCurrentUserAsync()); }
public async Task <JwtModel> AuthAsync(User user, string password) { if (user.Password == passwordEncrypter.Encrypt(password)) { await UpdateUserAttempts(user, attempts : 0); return(jwtService.GetToken(user)); } await UpdateUserAttempts(user, user.CountOfInvalidAttempts + 1); throw new ApplicationException("Неверный пароль."); }
public async Task <UserModel> RegisterAsync(RegisterUserModel registerUser) { var user = await usersService.GetByEmailAsync(registerUser.Email); if (user != null) { throw new ApplicationException("Пользователь с таким email уже существует."); } var verificationCode = GetVerificationCode(registerUser.Email); if (registerUser.VerificationCode?.Trim() != verificationCode.Trim()) { throw new ApplicationException("Неверный код подтверждения."); } var encryptedPassword = passwordEncrypter.Encrypt(registerUser.Password); user = new User { FirstName = registerUser.FirstName, LastName = registerUser.LastName, Email = registerUser.Email, Password = encryptedPassword, UserRoles = new List <UserRole> { new UserRole { Role = Role.Student } } }; usersRepository.Add(user); await dataContext.SaveChangesAsync(); return(UserMap.Map(user)); }
protected override async Task <Models.Account> InternalHandle(CreateAccountRequest request, CancellationToken cancellationToken) { if (await DbContext.Accounts.AnyAsync(x => x.Login == request.Account.Login, cancellationToken)) { throw new DuplicatedLoginDALException($"Account with login {request.Account.Login} already exist."); } var account = Mapper.Map <Account>(request.Account); account.PasswordHash = PasswordEncrypter.Encrypt(request.Account.Password); DbContext.Accounts.Add(account); await DbContext.SaveChangesAsync(cancellationToken); return(Mapper.Map <Models.Account>(account)); }
private void buttonSignIn_Click(object sender, EventArgs e) { if (textBoxUsername.Text.Length > 20) { MessageBox.Show("The username is too long!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (_dataAccess.SignIn(textBoxUsername.Text, _passwordEncrypter.Encrypt(textBoxPassword.Text), out MainForm.CurrentUser)) { Close(); } else { MessageBox.Show("There is no such user!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private IEnumerable <User> GetAdmins(SeedSettings settings, IPasswordEncrypter passwordEncrypter) { var password = passwordEncrypter.Encrypt(settings.DefaultAdmin.Password); yield return(new User() { Email = settings.DefaultAdmin.Email, FirstName = settings.DefaultAdmin.FirstName, LastName = settings.DefaultAdmin.LastName, Password = password, UserRoles = new List <UserRole> { new UserRole { Role = Role.Admin } } }); }