Ejemplo n.º 1
0
        public IActionResult AdminSettings(Config input)
        {
            if (ModelState.IsValid && HttpContext.Session.GetString("UserID") != null)
            {
                try
                {
                    VsAdmin user         = _db.VsAdmins.Find(input.AdminID);
                    string  userPassword = !string.IsNullOrEmpty(input.Password) ? hasher.HashPassword(user, input.Password) : user.Password;

                    user.Id        = input.AdminID;
                    user.Firstname = input.Firstname;
                    user.Lastname  = input.Lastname;
                    user.Email     = input.Email;
                    user.Password  = userPassword;

                    _db.VsAdmins.Update(user);
                    _db.SaveChanges();

                    // Return Json to support Ajax submits:
                    return(new JsonResult(input));
                }
                catch (Exception e)
                {
                    Console.Write(e.Message);
                }
            }

            // Get server side feedback:
            TempData["errors"] = ErrorFeedback();
            return(RedirectToAction(nameof(Settings)));
        }
Ejemplo n.º 2
0
        public ActionResult Resolution(int height, int width)
        {
            // collecting and storing the browser and resolution
            BrowserResolution record = new BrowserResolution()
            {
                BrowserResolutionHeight = height.ToString(),
                BrowserResolutionWidth = width.ToString(),
                IPAddress = Request.UserHostAddress,
                Browser = Request.Browser.Browser
            };

            // save the record to the tracking table.
            VisitorStatisticsContext context = new VisitorStatisticsContext();
            context.BrowserResolutionRecord.Add(record);
            context.SaveChanges();

            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }
Ejemplo n.º 3
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            // store the request in an accessible object
            var request = filterContext.HttpContext.Request;
            //generate a visitor statistic record.
            VisitorStatistic visitor = new VisitorStatistic()
            {
                UserName = (request.IsAuthenticated) ? filterContext.HttpContext.User.Identity.Name : "Annonymous",
                IPAddress = request.UserHostAddress ?? request.ServerVariables["HTTP_X_FORWARDED_FOR"],
                AlternateIPAddress = request.ServerVariables["REMOTE_ADDR"] ?? "Not Available",
                AreaAccessed = request.RawUrl,
                Timestamp = DateTime.UtcNow,
                Referer = request.ServerVariables["HTTP_REFERER"] ?? "internet",
                Browser = request.Browser.Browser
            };
            // stores the tracked info in the database
            VisitorStatisticsContext context = new VisitorStatisticsContext();
            context.VisitorStatisticRecords.Add(visitor);
            context.SaveChanges();

            //finished executing the action as normal
            base.OnActionExecuting(filterContext);
        }
        /// <summary>
        /// Save the visitor statistics passed from the middleware to the DB.
        /// </summary>
        /// <param name="input"></param>
        public void SaveVisitorStats(VisitorStats input)
        {
            try
            {
                VsVisitor visitor = new VsVisitor()
                {
                    Ipaddress  = input.IPAddress,
                    HostName   = input.HostName,
                    Agent      = input.Agent,
                    DeleteDate = input.DeletionDate,
                    AppId      = input.AppID
                };

                // Check if visitor (IP) is to be inserted or updated to DB:
                if (!_db.VsVisitors.Where(i => i.Ipaddress.Equals(input.IPAddress)).Any())
                {
                    _db.VsVisitors.Add(visitor);
                    _db.SaveChanges();
                }
                else
                {
                    visitor          = _db.VsVisitors.Where(v => v.Ipaddress.Equals(input.IPAddress)).Select(v => v).FirstOrDefault();
                    visitor.HostName = input.HostName;
                    visitor.Agent    = input.Agent;

                    _db.VsVisitors.Update(visitor);
                    _db.SaveChanges();
                }

                // Now ensure the visitor ID (to be used as reference key for visit entity) exists in DB:
                int visitorID = _db.VsVisitors.Where(i => i.Ipaddress.Equals(input.IPAddress)).Select(v => v.Id).First();

                if (visitorID > 0)
                {
                    VsVisit visit = new VsVisit
                    {
                        VisitorId  = visitorID,
                        RefererUrl = input.RefererURL,
                        VisitUrl   = input.VisitURL,
                        VisitTime  = input.VisitTime
                    };

                    _db.VsVisits.Add(visit);
                    _db.SaveChanges();

                    // Handle storage of admin visit:
                    if (_context.HttpContext.Session.GetString("AdminIP") != null)
                    {
                        if (_context.HttpContext.Session.GetString("AdminIP").Equals(input.IPAddress))
                        {
                            VsAdminVisit adminVisit = new VsAdminVisit()
                            {
                                // If admin IP session is set, then so is the user ID session (via HomeController > Login):
                                AdminId = Convert.ToInt32(_context.HttpContext.Session.GetString("UserID")),
                                VisitId = visit.Id
                            };

                            _db.VsAdminVisits.Add(adminVisit);
                            _db.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }
        }