Exemplo n.º 1
0
        public VisitorsStatisticsController(IVisitorsStatisticsService visitorsStatisticsService, IHttpRequestInfoService httpRequestInfoService)
        {
            _visitorsStatisticsService = visitorsStatisticsService;
            _visitorsStatisticsService.CheckArgumentIsNull(nameof(_visitorsStatisticsService));

            _httpRequestInfoService = httpRequestInfoService;
            _httpRequestInfoService.CheckArgumentIsNull(nameof(_httpRequestInfoService));
        }
Exemplo n.º 2
0
        public ViewersStatistics(IVisitorsStatisticsService statService, IHttpRequestInfoService httpRequestInfoService, IHtmlHelperService htmlHelperService)
        {
            _statService = statService;
            _statService.CheckArgumentIsNull(nameof(_statService));

            _httpRequestInfoService = httpRequestInfoService;

            _htmlHelperService = htmlHelperService;
        }
Exemplo n.º 3
0
        public async Task Invoke(HttpContext context, IVisitorsStatisticsService statService)
        {
            string visitorId = context.Request?.Cookies["VisitorId"];

            if (visitorId == null)
            {
                if (!VisitorsStatisticsHelper.IsBotOrCrawler(context.Request?.Headers[UserAgent].ToString()))
                {
                    var userAgent   = context.Request?.Headers[UserAgent].ToString();
                    var userIp      = context.Connection?.RemoteIpAddress?.ToString();
                    var userOs      = VisitorsStatisticsHelper.GetUserOsName(userAgent);
                    var browserName = VisitorsStatisticsHelper.GetUserBrowserName(userAgent);
                    var deviceName  = VisitorsStatisticsHelper.GetUserDeviceName(userAgent);
                    var referer     = context.Request?.Headers["Referer"].ToString() ?? "Direct";

                    var viewModel = new VisitorsStatisticsViewModel()
                    {
                        UserAgent   = userAgent,
                        UserOs      = userOs.Family,
                        BrowserName = browserName.Family,
                        DeviceName  = deviceName.Family,
                        IpAddress   = userIp,
                        PageViewed  = context.Request?.Path.Value ?? "/",
                        Referrer    = referer,
                        VisitDate   = DateTimeOffset.UtcNow,
                    };

                    await statService.InsertAsync(viewModel);

                    context.Response.Cookies.Append("VisitorId", Guid.NewGuid().ToString("N"), new CookieOptions()
                    {
                        Path     = "/",
                        HttpOnly = true,
                        Secure   = context.Request?.IsHttps ?? false,
                    });
                }
            }

            await _next(context);
        }