public bool Login() { UnicodeEncoding encoding = new UnicodeEncoding(); using (TedTechVPNEntities dbContext = new TedTechVPNEntities()) { User user = dbContext.User.FirstOrDefault(u => u.Name == User.Name); if (user == null || user.Password != encoding.GetString(_passwordProvider.Hash(SecurePassword, encoding.GetBytes(user.Salt)))) { return(false); } User.IsPrivileged = user.IsPrivileged; return(true); } }
public async Task <Result> Handle(LoginQuery request, CancellationToken cancellationToken) { Result result; var passwordHash = _passwordProvider.Hash(request.Password); try { var user = await _userFinder.Get(request.Login); var passwordMatch = _passwordProvider.Verify(request.Password, user.Password); if (passwordMatch) { result = Result.Ok(new AuthenticatedUserModel { Id = user.Id }); } else { throw new EntityNotFoundDbException(); } } catch (EntityNotFoundDbException) { result = Result.Fail(new System.Collections.Generic.List <Failure>() { new HandlerFault() { Code = HandlerFaultCode.InvalidCredentials.Name, Message = HandlerFailures.InvalidCredentials, Target = "credentials" } } ); } catch { result = Result.Fail(CustomFailures.LoginUserFailure); } return(result); }
public async Task <Result> Handle(CreateCommand command, CancellationToken cancellationToken) { var id = _identifierProvider.Generate(); var login = new Login(command.Login); var passwordHashed = _passwordProvider.Hash(command.Password); var firstName = new FirstName(command.FirstName); var lastName = new LastName(command.LastName); var fullName = new FullName(firstName, lastName); var userToCreate = new User(id, login, passwordHashed, fullName); userToCreate.Version = _versionProvider.Generate(); Result result; try { await _userRepository.Create(userToCreate); result = Result.Ok(id, userToCreate.Version); await _publishIntegrationEventsService.PublishUserCreated(id, command.Login, command.FirstName, command.LastName); } catch (UniqueKeyException) { result = Result.Fail(new System.Collections.Generic.List <Failure>() { new HandlerFault() { Code = HandlerFaultCode.Conflict.Name, Message = HandlerFailures.Conflict, Target = "login" } } ); } catch { result = Result.Fail(CustomFailures.CreateUserFailure); } return(result); }
public void Hash() { //arrange byte[] salt = { 76, 38, 64, 235, 154 }; SecureString password = new SecureString(); foreach (char c in "1234") { password.AppendChar(c); } byte[] expectedHash = { 77, 181, 159, 218, 11, 68, 102, 46, 4, 191, 206, 197, 224, 118, 75, 51, 28, 140, 222, 158, 3, 22, 240, 211, 181, 189, 96, 8, 213, 215, 141, 15, 109, 216, 68, 191, 155, 186,175, 239, 186, 63, 28, 226, 114, 237, 5, 109, 179, 198, 57, 163, 139, 10, 88, 188, 206, 28, 241, 39, 121, 8, 70, 152 }; //act byte[] actualHash = _provider.Hash(password, salt); //assert actualHash.Should().BeEquivalentTo(expectedHash); }
public async Task <Result> Handle(UpdateCommand command, CancellationToken cancellationToken) { Result result; try { var user = await _userFinder.Get(command.Id); if (user.Version != command.Version) { throw new CommandVersionException(); } if (command.Login.HasValue) { var updatedLogin = command.Login.Value; user.ChangeLogin(new Login(updatedLogin)); } if (command.Password.HasValue) { var passwordHashed = _passwordProvider.Hash(command.Password.Value); user.ChangePassword(passwordHashed); } if (command.FirstName.HasValue) { user.FullName.ChangeFirstName(command.FirstName.Value); } if (command.LastName.HasValue) { user.FullName.ChangeLastName(command.LastName.Value); } user.Version = _versionProvider.Generate(); await _userRepository.Update(user); result = Result.Ok(user.Version); await _publishIntegrationEventsService.PublishUserUpdated(user.Id, user.Login.Value, user.FullName.FirstName.Value, user.FullName.LastName.Value); } catch (EntityNotFoundDbException) { result = Result.Fail(new System.Collections.Generic.List <Failure>() { new HandlerFault() { Code = HandlerFaultCode.NotFound.Name, Message = HandlerFailures.NotFound, Target = "id" } } ); } catch (CommandVersionException) { result = Result.Fail(new System.Collections.Generic.List <Failure>() { new HandlerFault() { Code = HandlerFaultCode.NotMet.Name, Message = HandlerFailures.NotMet, Target = "version" } } ); } catch (UniqueKeyException) { result = Result.Fail(new System.Collections.Generic.List <Failure>() { new HandlerFault() { Code = HandlerFaultCode.Conflict.Name, Message = HandlerFailures.Conflict, Target = "login" } } ); } catch { result = Result.Fail(CustomFailures.UpdateUserFailure); } return(result); }