public async Task Consume(ConsumeContext <AuthValidationMQRequest> context) { try { AuthValidationMQResponse response = new AuthValidationMQResponse(); try { response.OpSuccess = true; AuthValidationRequest request = new AuthValidationRequest() { CorrelationId = context.Message.CorrelationId, UsernameOrEmail = context.Message.UsernameOrEmail, Password = context.Message.Password }; (bool Result, string AccountId)validationRes = await _validateCredentialsCommand.Execute(request); if (validationRes.Result) { response.IsValid = true; response.AccountId = validationRes.AccountId; } else { response.IsValid = false; } await _logger.LogEventAsync("ValidateCredentialsConsumer.Consume", $"Validation request for {context.Message.UsernameOrEmail} served."); } catch (Exception ex) { //Log error await _logger.LogErrorAsync("ValidateCredentialsConsumer.Consume", "Exception was thrown.", new { ConsumeContext = context, Exception = ex }); response.OpSuccess = false; } await context.RespondAsync <AuthValidationMQResponse>(response); } catch (Exception ex) { //Log error await _logger.LogErrorAsync("ValidateCredentialsConsumer.Consume", "Exception was thrown", new { ConsumeContext = context, Exception = ex }); } }
public async Task <(bool Result, string AccountId)> Execute(AuthValidationRequest request) { try { if ((request == null) || (request.UsernameOrEmail == null) || (request.Password == null)) { throw new ArgumentNullException("Credentials were not provided"); } //Retrieve account and check existence Account account = _accountRepo.Find(a => a.Username == request.UsernameOrEmail || a.Email == request.UsernameOrEmail) .FirstOrDefault(); if (account == null) { return(false, null); } //Validate password if (!_hashHelper.ValidateSecret(account.Salt + request.Password, account.Password)) { return(false, null); } return(true, account.AccountId); } catch (Exception ex) { //Log error await _logger.LogErrorAsync("ValidateCredentialsCommand.Execute", "Exception was thrown", new { AuthValidationRequest = request, Exception = ex }); throw; } }