public async System.Threading.Tasks.Task Test_RegisterUser()
        {
            UserInComingDto newUser = new UserInComingDto()
            {
                Email    = "*****@*****.**",
                Name     = this.mockName,
                Password = this.mockName
            };

            AuthenticationService authenticationService = new AuthenticationService(_mapper, _unitOfWorkMock.Object, _loggerFactory.CreateLogger <IAuthenticationService>(), _tokenHandler.Object);

            Assert.NotNull(authenticationService.RegisterUser(newUser));
        }
        public async System.Threading.Tasks.Task Test_RegisterUser_EmailExist(string email)
        {
            UserInComingDto newUser = new UserInComingDto()
            {
                Email    = email,
                Name     = this.mockName,
                Password = this.mockPassword
            };

            AuthenticationService authenticationService = new AuthenticationService(_mapper, _unitOfWorkMock.Object, _loggerFactory.CreateLogger <IAuthenticationService>(), _tokenHandler.Object);

            Assert.Throws <InvalidInputException>(() => authenticationService.RegisterUser(newUser));
        }
        public async System.Threading.Tasks.Task Test_LoginUser_InvalidEmail()
        {
            UserInComingDto newUser = new UserInComingDto()
            {
                Email    = "*****@*****.**",
                Name     = this.mockName,
                Password = this.mockPassword
            };

            AuthenticationService authenticationService = new AuthenticationService(_mapper, _unitOfWorkMock.Object, _loggerFactory.CreateLogger <IAuthenticationService>(), _tokenHandler.Object);

            Assert.Throws <InvalidInputException>(() => authenticationService.AuthenicateUser(newUser));
        }
        public async System.Threading.Tasks.Task Test_LoginUser(string email)
        {
            UserInComingDto newUser = new UserInComingDto()
            {
                Email    = email,
                Name     = this.mockName,
                Password = this.mockPassword
            };

            AuthenticationService authenticationService = new AuthenticationService(_mapper, _unitOfWorkMock.Object, _loggerFactory.CreateLogger <IAuthenticationService>(), _tokenHandler.Object);
            TokenDto result = authenticationService.AuthenicateUser(newUser);

            Assert.NotNull(result);
            Assert.NotNull(result.Token);
        }
Beispiel #5
0
 public async Task <ActionResult <UserOutGoingDto> > Register([FromBody] UserInComingDto userInComingData)
 {
     try
     {
         _authenicationService.RegisterUser(userInComingData);
         return(Ok());
     }catch (InvalidInputException invalidInputException)
     {
         _logger.LogInformation($"Could not register user", userInComingData, invalidInputException);
         return(BadRequest(_localizer["RegisterUser_Error"].Value));
     }
     catch (Exception exception)
     {
         _logger.LogError($"Could not register user", userInComingData, exception);
         return(StatusCode(500, _localizer["InternalError"].Value));
     }
 }
Beispiel #6
0
        public ActionResult <string> Authenticate([FromBody] UserInComingDto userInComingData)
        {
            try
            {
                TokenDto token = _authenicationService.AuthenicateUser(userInComingData);

                return(Ok(token));
            }
            catch (InvalidInputException invalidInputException)
            {
                _logger.LogInformation($"Could not authenitcate user", userInComingData, invalidInputException);
                return(BadRequest(_localizer["AuthenticateUser_Error"].Value));
            }
            catch (Exception exception)
            {
                _logger.LogError($"Could not authenitcate user", userInComingData, exception);
                return(StatusCode(500, _localizer["InternalError"].Value));
            }
        }
        public User RegisterUser(UserInComingDto userInComingDto)
        {
            CreatePasswordHash(userInComingDto.Password, out byte[] passwordHash, out byte[] passwordSalt);

            User newUser = _mapper.Map <User>(userInComingDto);

            newUser.PasswordHash = passwordHash;
            newUser.PasswordSalt = passwordSalt;

            try
            {
                _unitOfWork.UserRepository.AddUser(newUser);
                _unitOfWork.SaveChanges();
                _logger.LogInformation($"User {newUser.Name} with email: {newUser.Email} is registered|");
                return(newUser);
            }catch (Exception ex)
            {
                _logger.LogError($"Could not register user {newUser.Name} with email: {newUser.Email}");
                throw ex;
            }
        }
        public TokenDto AuthenicateUser(UserInComingDto userIncomingData)
        {
            User user = _unitOfWork.UserRepository.GetUser(userIncomingData.Email);

            bool userExist        = user != default(User);
            bool passwordVerified = VerifyPasswordHash(userIncomingData.Password, user.PasswordHash, user.PasswordSalt);

            if (!userExist || !passwordVerified)
            {
                _logger.LogTrace($"A user with email {userIncomingData.Email} tride to login but failed.");
                throw new InvalidInputException("Email and/or password is incorrect");
            }

            string token = _tokenHandler.CreateAuthenticationToken(user);

            TokenDto tokenDto = new TokenDto();

            tokenDto.Token = token;

            return(tokenDto);
        }
Beispiel #9
0
 public void UpdateUser(int Id, UserInComingDto changedUserInComingData)
 {
     throw new NotImplementedException();
 }
        public bool UserExist(UserInComingDto userInComingDto)
        {
            User user = _unitOfWork.UserRepository.GetUser(userInComingDto.Email);

            return(user != default(User));
        }