public async Task <Person> RegisterUserAsync(string login, string password, string email) { if ((await _personRepository.GetByLoginAsync(login)) != null) { throw new UserAlreadyExistsException(); } byte[] salt = logic.GenerateSalt(); var encryptedPassword = logic.EncryptPassword(password, salt); var person = new Person { Login = login, Password = encryptedPassword, Email = email, salt = salt, }; person = _personRepository.Insert(person); await _personRepository.UnitOfWork.SaveChangesAsync(); return(person); }
public async Task ChangePersonPassword(string userName, string password) { var currentUser = await _personRepository.GetByLoginAsync(userName); AuthenticationLogic logic = new AuthenticationLogic(); var salt = logic.GenerateSalt(); var encryptedPassword = logic.EncryptPassword(password, salt); currentUser.Password = encryptedPassword; currentUser.salt = salt; await _personRepository.UnitOfWork.SaveChangesAsync(); }
public async Task AuthenticateUserAsync_UserExistsAndPasswordIsCorrect_ReturnUser(string login, string password) { var logic = new AuthenticationLogic(); var Salt = logic.GenerateSalt(); var user = new Person { Login = login, Password = logic.EncryptPassword(password, Salt), salt = Salt }; var repositoryMock = new Mock <IPersonRepository>(); repositoryMock.Setup(x => x.GetByLoginAsync(It.IsAny <string>())) .Returns(Task.FromResult(user)); var service = new AuthenticationService(repositoryMock.Object); var result = await service.AuthenticateUserAsync(login, password); Assert.IsType <Person>(result); }