예제 #1
0
        public async Task <UserTicketModel> AuthenticateAsync(QuestionAuthModel authModel)
        {
            List <UserQuestion> userQuestions = (await _unitOfWork
                                                 .AuthenticationRepository
                                                 .GetUserQuestionsAsync(authModel.UserName)
                                                 ).ToList();

            bool loginFailed = false;

            foreach (var questionAnswer in authModel.QuestionAnswers)
            {
                UserQuestion userQuestion =
                    userQuestions
                    .FirstOrDefault(uq => uq.QuestionId == questionAnswer.Id);
                if (questionAnswer.Answer != userQuestion.Answer)
                {
                    loginFailed = true;
                    break;
                }
            }
            if (loginFailed)
            {
                _unitOfWork.JournalRepository
                .AddOperationEntryAsync(
                    new OperationJournal
                {
                    UserId       = userQuestions[0].UserId,
                    LoginSuccess = false
                }
                    );
                await _unitOfWork.Save();

                return(new UserTicketModel {
                    Id = 0
                });
            }
            _unitOfWork.JournalRepository
            .AddRegistrationEntryAsync(
                new RegistrationJournal
            {
                UserId = userQuestions[0].UserId
            }
                );
            _unitOfWork.JournalRepository
            .AddOperationEntryAsync(
                new OperationJournal
            {
                UserId       = userQuestions[0].UserId,
                LoginSuccess = true
            }
                );
            await _unitOfWork.Save();

            return(new UserTicketModel
            {
                Id = userQuestions[0].UserId,
                UserName = authModel.UserName
            });
        }
예제 #2
0
        public async Task <IActionResult> Authenticate(QuestionAuthModel authModel)
        {
            UserTicketModel ticket =
                await _authenticationService.AuthenticateAsync(authModel);

            if (ticket.Id == 0)
            {
                return(BadRequest());
            }
            return(Ok(ticket));
        }