public async Task TrackAuthenticatedVisitAsync(VisitorTrackingModel visitorTrackingModel,
                                                       bool createNewSession)
        {
            if (createNewSession)
            {
                SessionId = Guid.NewGuid();
            }
            visitorTrackingModel.SessionId = SessionId;
            var authorizedHttpClient = this.HttpClientService.CreateAuthorizedClient();
            var response             = await authorizedHttpClient.PostAsJsonAsync(ApiRoutes.VisitorTrackingController.TrackAuthenticatedClientInformation, visitorTrackingModel);

            if (!response.IsSuccessStatusCode)
            {
                ProblemHttpResponse problemHttpResponse = await response.Content.ReadFromJsonAsync <ProblemHttpResponse>();

                if (problemHttpResponse != null)
                {
                    throw new CustomValidationException(problemHttpResponse.Detail);
                }
                else
                {
                    throw new CustomValidationException(response.ReasonPhrase);
                }
            }
            else
            {
                visitorTrackingModel = await response.Content.ReadFromJsonAsync <VisitorTrackingModel>();

                this.VisitorTrackingId = visitorTrackingModel.VisitorTrackingId;
            }
        }
Beispiel #2
0
        public async Task <VisitorTrackingModel> TrackAnonymousClientInformation(VisitorTrackingModel visitorTrackingModel)
        {
            var response = await this.VisitorTrackingService.TrackVisitAsync(visitorTrackingModel);

            if (response != null)
            {
                visitorTrackingModel = this.Mapper.Map <VisitorTracking, VisitorTrackingModel>(response);
                return(visitorTrackingModel);
            }
            return(null);
        }
Beispiel #3
0
        public async Task <VisitorTrackingModel> TrackAuthenticatedClientInformation(
            VisitorTrackingModel visitorTrackingModel, [FromServices] ICurrentUserProvider currentUserProvider)
        {
            var userObjectId = currentUserProvider.GetObjectId();

            visitorTrackingModel.UserAzureAdB2cObjectId = userObjectId;
            var response = await this.VisitorTrackingService.TrackVisitAsync(visitorTrackingModel);

            if (response != null)
            {
                visitorTrackingModel = this.Mapper.Map <VisitorTracking, VisitorTrackingModel>(response);
                return(visitorTrackingModel);
            }
            return(null);
        }
        public async Task TrackAnonymousVisitAsync(VisitorTrackingModel visitorTrackingModel,
                                                   bool createNewSession)
        {
            if (createNewSession)
            {
                SessionId = Guid.NewGuid();
            }
            visitorTrackingModel.SessionId = SessionId;
            var anonymousHttpClient = this.HttpClientService.CreateAnonymousClient();
            var response            = await anonymousHttpClient.PostAsJsonAsync(ApiRoutes.VisitorTrackingController.TrackAnonymousClientInformation, visitorTrackingModel);

            if (!response.IsSuccessStatusCode)
            {
                try
                {
                    ProblemHttpResponse problemHttpResponse = await response.Content.ReadFromJsonAsync <ProblemHttpResponse>();

                    if (problemHttpResponse != null)
                    {
                        throw new CustomValidationException(problemHttpResponse.Detail);
                    }
                    else
                    {
                        throw new CustomValidationException(response.ReasonPhrase);
                    }
                }
                catch (Exception)
                {
                    string errorText = await response.Content.ReadAsStringAsync();

                    string reasonPhrase = response.ReasonPhrase;
                    throw new CustomValidationException($"{reasonPhrase} - {errorText}");
                }
            }
            else
            {
                visitorTrackingModel = await response.Content.ReadFromJsonAsync <VisitorTrackingModel>();

                this.VisitorTrackingId = visitorTrackingModel.VisitorTrackingId;
            }
        }
        public async Task <VisitorTracking> TrackVisitAsync(VisitorTrackingModel visitorTrackingModel)
        {
            try
            {
                var httpContext     = HttpContextAccessor.HttpContext;
                var remoteIpAddress = httpContext.Connection.RemoteIpAddress.ToString();
                if (remoteIpAddress == "::1")
                {
                    var ipAddresses = await IpAddressProvider.GetCurrentHostIPv4AddressesAsync();

                    remoteIpAddress = ipAddresses.First();
                }
                var parsedIpAddress   = System.Net.IPAddress.Parse(remoteIpAddress);
                var ipGeoLocationInfo = await IpDataService.GetIpGeoLocationInfoAsync(ipAddress : parsedIpAddress);

                //var ipGeoLocationInfo = await IpStackService.GetIpGeoLocationInfoAsync(ipAddress: parsedIpAddress);
                string          country    = ipGeoLocationInfo.country_name;
                var             host       = httpContext.Request.Host.Value;
                var             userAgent  = httpContext.Request.Headers["User-Agent"].First();
                ApplicationUser userEntity = null;
                if (!String.IsNullOrWhiteSpace(visitorTrackingModel.UserAzureAdB2cObjectId))
                {
                    userEntity = await this.FairplaytubeDatabaseContext.ApplicationUser.SingleOrDefaultAsync(p => p.AzureAdB2cobjectId.ToString() == visitorTrackingModel.UserAzureAdB2cObjectId);
                }
                var visitedPage = new DataAccess.Models.VisitorTracking()
                {
                    ApplicationUserId = userEntity?.ApplicationUserId,
                    Country           = country,
                    Host            = host,
                    RemoteIpAddress = remoteIpAddress,
                    UserAgent       = userAgent,
                    VisitDateTime   = DateTimeOffset.UtcNow,
                    VisitedUrl      = visitorTrackingModel.VisitedUrl,
                    SessionId       = visitorTrackingModel.SessionId
                };
                await this.FairplaytubeDatabaseContext.VisitorTracking.AddAsync(visitedPage);

                await this.FairplaytubeDatabaseContext.SaveChangesAsync();

                var pageUri     = new Uri(visitedPage.VisitedUrl);
                var lastSegment = pageUri.Segments.Last().TrimEnd('/');
                if (!String.IsNullOrWhiteSpace(lastSegment))
                {
                    var videoInfoEntity = await this.FairplaytubeDatabaseContext.VideoInfo.SingleOrDefaultAsync(p => p.VideoId == lastSegment);

                    if (videoInfoEntity != null)
                    {
                        visitedPage.VideoInfoId = videoInfoEntity.VideoInfoId;
                        await this.FairplaytubeDatabaseContext.SaveChangesAsync();
                    }
                }
                return(visitedPage);
            }
            catch (Exception ex)
            {
                try
                {
                    await this.FairplaytubeDatabaseContext.ErrorLog.AddAsync(new ErrorLog()
                    {
                        FullException = ex.ToString(),
                        Message       = ex.Message,
                        StackTrace    = ex.StackTrace
                    });

                    await this.FairplaytubeDatabaseContext.SaveChangesAsync();
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return(null);
        }