public LoginResultViewModel Login(LoginViewModel model)
        {
            _logger.Debug("Calling CandidateServiceProvider to log the user {0}",
                          model.EmailAddress);

            try
            {
                var userStatusViewModel = GetUserStatus(model.EmailAddress);

                if (userStatusViewModel.UserStatus.HasValue)
                {
                    if (userStatusViewModel.UserStatus == UserStatuses.Locked)
                    {
                        return(GetLoginResultViewModel(model, userStatusViewModel.UserStatus.Value));
                    }

                    var candidate = _candidateService.Authenticate(model.EmailAddress, model.Password);

                    if (candidate != null)
                    {
                        // User is authentic.
                        SetUserCookies(candidate, _userAccountService.GetRoleNames(model.EmailAddress));

                        return(new LoginResultViewModel
                        {
                            EmailAddress = candidate.RegistrationDetails.EmailAddress,
                            FullName = candidate.RegistrationDetails.FirstName + " " + candidate.RegistrationDetails.LastName,
                            UserStatus = userStatusViewModel.UserStatus.Value,
                            IsAuthenticated = true,
                            AcceptedTermsAndConditionsVersion = candidate.RegistrationDetails.AcceptedTermsAndConditionsVersion,
                            MobileVerificationRequired = candidate.MobileVerificationRequired()
                        });
                    }

                    userStatusViewModel = GetUserStatus(model.EmailAddress);

                    if (userStatusViewModel.UserStatus == UserStatuses.Locked)
                    {
                        // Authentication failed, user just locked their account.
                        return(GetLoginResultViewModel(model, userStatusViewModel.UserStatus.Value));
                    }
                }

                return(GetAuthenticationFailedViewModel(model, userStatusViewModel.UserStatus));
            }
            catch (Exception e)
            {
                _logger.Error("Candidate login failed for " + model.EmailAddress, e);

                return(new LoginResultViewModel(LoginPageMessages.LoginFailedErrorText)
                {
                    EmailAddress = model.EmailAddress
                });
            }
        }
        public IActionResult Authenticate([FromBody] CandidateDto candidateDto)
        {
            _logger.LogInformation("Authentication");

            var authentication = _candidateService.Authenticate(candidateDto.Email, candidateDto.Password);

            _logger.LogInformation("Authentication result: " + authentication);

            if (authentication == 1)
            {
                return(BadRequest(new { message = "Neteisingai įvestas elektroninio pašto adresas arba slaptažodis. Pataisykite ir bandykite prisijungti dar kartą." }));
            }

            if (authentication == 2)
            {
                return(BadRequest(new { message = "Šio interviu galiojimo laikas baigėsi." }));
            }

            if (authentication == 3)
            {
                return(BadRequest(new { message = "Šis interviu jau buvo atliktas." }));
            }

            // Prepare JWT token
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.JwtSecret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, candidateDto.Email),
                    new Claim(ClaimTypes.Role, "Candidate")
                }),
                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            // Retriev application
            var application = _candidateService.GetApplicationId(candidateDto.Email, candidateDto.Password);

            // Return
            return(Ok(new {
                Username = application.CandidateName,
                Email = application.CandidateEmail,
                ApplicationId = application.Id,
                Token = tokenString
            }));
        }
Example #3
0
        public MediatorResponse VerifyAccountSettings(Guid candidateId, DeleteAccountSettingsViewModel deleteAccountSettingsViewModel)
        {
            var validationResult = _deleteAccountSettingsViewModelServerValidator.Validate(deleteAccountSettingsViewModel);

            try
            {
                if (!validationResult.IsValid)
                {
                    return(GetMediatorResponse(AccountMediatorCodes.ValidateUserAccountBeforeDelete.ValidationError, deleteAccountSettingsViewModel, validationResult));
                }
                var candidate = _candidateService.Authenticate(deleteAccountSettingsViewModel.EmailAddress, deleteAccountSettingsViewModel.Password);
                if (candidate != null && candidate.EntityId == candidateId)
                {
                    return(GetMediatorResponse(AccountMediatorCodes.ValidateUserAccountBeforeDelete.Ok, deleteAccountSettingsViewModel));
                }
            }
            catch (Exception)
            {
                return(GetMediatorResponse(AccountMediatorCodes.ValidateUserAccountBeforeDelete.HasError, deleteAccountSettingsViewModel, validationResult, MyApplicationsPageMessages.InvalidUserAccount, UserMessageLevel.Error));
            }
            return(GetMediatorResponse(AccountMediatorCodes.ValidateUserAccountBeforeDelete.HasError, deleteAccountSettingsViewModel, validationResult, MyApplicationsPageMessages.InvalidUserAccount, UserMessageLevel.Error));
        }
        public LoginResultViewModel Login(LoginViewModel model)
        {
            _logger.Debug("Calling CandidateServiceProvider to log the user {0}",
                          model.EmailAddress);

            try
            {
                var user = _userAccountService.GetUser(model.EmailAddress, false);

                if (user == null || user.Status == UserStatuses.PendingDeletion)
                {
                    return(GetAuthenticationFailedViewModel(model));
                }

                if (user.Status == UserStatuses.Locked)
                {
                    return(GetLoginResultViewModel(model, user.Status));
                }

                var candidate = _candidateService.Authenticate(model.EmailAddress, model.Password);

                if (candidate != null)
                {
                    // User is authentic.
                    _authenticationTicketService.SetAuthenticationCookie(candidate.EntityId.ToString(), _userAccountService.GetRoleNames(candidate.EntityId));

                    _userDataProvider.Push(UserDataItemNames.LastApplicationStatusNotification,
                                           user.LastLogin.HasValue ? user.LastLogin.Value.Ticks.ToString(CultureInfo.InvariantCulture) : DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture));

                    return(new LoginResultViewModel
                    {
                        EmailAddress = candidate.RegistrationDetails.EmailAddress,
                        FullName = candidate.RegistrationDetails.FirstName + " " + candidate.RegistrationDetails.LastName,
                        UserStatus = user.Status,
                        IsAuthenticated = true,
                        AcceptedTermsAndConditionsVersion = candidate.RegistrationDetails.AcceptedTermsAndConditionsVersion,
                        PhoneNumber = candidate.RegistrationDetails.PhoneNumber,
                        MobileVerificationRequired = candidate.MobileVerificationRequired(),
                        PendingUsernameVerificationRequired = user.PendingUsernameVerificationRequired()
                    });
                }

                user = _userAccountService.GetUser(model.EmailAddress);

                if (user.Status == UserStatuses.Locked)
                {
                    // Authentication failed, user just locked their account.
                    return(GetLoginResultViewModel(model, user.Status));
                }

                return(GetAuthenticationFailedViewModel(model, user.Status));
            }
            catch (Exception e)
            {
                _logger.Error("Candidate login failed for " + model.EmailAddress, e);

                return(new LoginResultViewModel(LoginPageMessages.LoginFailedErrorText)
                {
                    EmailAddress = model.EmailAddress
                });
            }
        }