public async Task <bool> InsertAsync(VisitorsStatisticsViewModel viewModel) { if (string.IsNullOrEmpty(viewModel.Referrer)) { viewModel.Referrer = "Direct"; } var entity = new VisitorsStatistics() { Id = viewModel.Id, UserAgent = viewModel.UserAgent, UserOs = viewModel.UserOs, BrowserName = viewModel.BrowserName, DeviceName = viewModel.DeviceName, IpAddress = viewModel.IpAddress, PageViewed = viewModel.PageViewed, Referrer = viewModel.Referrer, VisitDate = viewModel.VisitDate, }; await _statistics.AddAsync(entity); var result = await _unitOfWork.SaveChangesAsync(); return(result != 0); }
public async Task OnActionExecutionAsync( ActionExecutingContext context, ActionExecutionDelegate next) { // Do something before the action executes. var userAgent = context.HttpContext?.Request?.Headers["User-Agent"].ToString(); var userIp = context.HttpContext?.Connection?.RemoteIpAddress?.ToString(); var userOs = VisitorsStatisticsHelper.GetUserOsName(userAgent); var browserName = VisitorsStatisticsHelper.GetUserBrowserName(userAgent); var deviceName = VisitorsStatisticsHelper.GetUserDeviceName(userAgent); //var url = Url.Action("Index", "Home", null, ViewContext.HttpContext.Request.Scheme); var getIp = _httpRequestInfoService.GetIP(); var getHeaderValue = _httpRequestInfoService.GetHeaderValue("Accept-Language"); var getUserAgent = _httpRequestInfoService.GetUserAgent(); var absoluteContent = _httpRequestInfoService.AbsoluteContent("~/"); var absoluteContent2 = _httpRequestInfoService.AbsoluteContent(context.HttpContext?.Request?.Path.Value ?? "~/"); var getBaseUrl = _httpRequestInfoService.GetBaseUrl(); var getRawUrl = _httpRequestInfoService.GetRawUrl(); //var getUrlTitleAsync = await _htmlHelperService.GetUrlTitleAsync(context.HttpContext?.Request?.Path.Value ?? "~/"); var getUrlHelper = _httpRequestInfoService.GetUrlHelper().Content(context.HttpContext?.Request?.Path.Value ?? "~/"); var viewModel = new VisitorsStatisticsViewModel() { UserAgent = userAgent, UserOs = userOs.ToString(), BrowserName = browserName.ToString(), DeviceName = deviceName.ToString(), IpAddress = userIp, PageViewed = null, VisitDate = DateTimeOffset.UtcNow, }; await _statService.InsertAsync(viewModel); // next() calls the action method. var resultContext = await next(); // resultContext.Result is set. // Do something after the action executes. }
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); }