コード例 #1
0
        public async Task <AuthenticationResponse> Authenticate(AuthenticationRequest request)
        {
            // TODO: [TESTS] (UserService.Authenticate) Add tests
            var builder = new ServiceMetricBuilder(nameof(UserService), nameof(Authenticate))
                          .WithCategory(MetricCategory.User, MetricSubCategory.GetSingle)
                          .WithCustomTag1(request.Username);

            try
            {
                using (builder.WithTiming())
                {
                    UserEntity loggedInUser;
                    using (builder.WithCustomTiming1())
                    {
                        builder.IncrementQueryCount();
                        loggedInUser = await LoginUser(request.Username, request.Password);

                        builder.CountResult(loggedInUser);
                    }

                    if (loggedInUser == null)
                    {
                        return(null);
                    }

                    builder.WithCustomInt1(loggedInUser.UserId);

                    return(new AuthenticationResponse
                    {
                        FirstName = loggedInUser.FirstName,
                        LastName = loggedInUser.LastName,
                        UserId = loggedInUser.UserId,
                        Username = loggedInUser.Username,
                        Token = GenerateJwtToken(loggedInUser.UserId)
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.LogUnexpectedException(ex);
                builder.WithException(ex);
                return(null);
            }
            finally
            {
                await _metrics.SubmitPointAsync(builder.Build());
            }
        }