Esempio n. 1
0
        public async Task <(Guid token, DateTime until)> AuthenticateAsync(string username, string password)
        {
            try
            {
                var user = await _userService.GetUserByUsernameAsync(username);

                if (user == null)
                {
                    throw new UnauthorizedAccessException("User not found");
                }

                var isValidPassword = _passwordHelper.ValidatePassword(user.HashedPassword, password);

                if (!isValidPassword)
                {
                    throw new UnauthorizedAccessException("Password incorrect");
                }

                return(_tokenHelper.SetToken(user));
            }
            catch (Exception e) when(!(e is UnauthorizedAccessException))
            {
                _logger.WriteLog(LogLevel.Error, "An exception occurred in Authenticator.AuthenticateAsync", e.Message,
                                 e.StackTrace);
                throw;
            }
        }
Esempio n. 2
0
        public async Task <bool> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            var staffAccount = _staffRepo.GetByEmail(request.Email);

            if (staffAccount == null)
            {
                return(false);
            }

            var isMatching =
                _passwordHelper.ValidatePassword(request.Password, staffAccount.PasswordSalt, staffAccount.PasswordHash);

            if (!isMatching)
            {
                return(false);
            }

            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, staffAccount.Name),
                new Claim(ClaimTypes.NameIdentifier, staffAccount.Id.ToString()),
                new Claim(ClaimTypes.Role, staffAccount.Role.ToString()),
            };

            var userIdentity  = new ClaimsIdentity(claims, "Basic");
            var userPrincipal = new ClaimsPrincipal(userIdentity);

            await _signInManager.SignInAsync(userPrincipal, false);

            _cache.Set(staffAccount.Id.ToString(), staffAccount.Role.ToString(), CacheHelper.CacheOptions());

            return(true);
        }
Esempio n. 3
0
        public User Login(User login)
        {
            var user = _uow.Users.FirstOrDefault(a => a.Email.ToLower() == login.Email.Trim().ToLower(), "");

            if (user == null)
            {
                throw new ValidationException("Email not exist.");
            }

            if (!_passHelper.ValidatePassword(login.Password, user.Password))
            {
                throw new ValidationException("Wrong email or password.");
            }

            return(user);
        }
Esempio n. 4
0
        public User ValidateUser(string username, string password)
        {
            User user;

            try
            {
                user = _drinksContext.Users.Single(x => x.Username == username);
            }
            catch (InvalidOperationException e)
            {
                throw new InvalidUserCredentialsException(e);
            }

            if (_passwordHelper.ValidatePassword(password, user.Salt, user.Password))
            {
                return(user);
            }

            throw new InvalidUserCredentialsException();
        }