// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Visitor).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!VisitorExists(Visitor.VisitorID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(string site) { FormSubmitted = true; FormSaved = true; string FormErrors = null; Site = (await _context.Site .FromSqlInterpolated($"EXEC SPR_VIS_SiteDetails @Site={site}") .ToListAsync()) .FirstOrDefault(); if (Site == null) { SiteIsValid = false; SiteName = "Unknown"; } else { SiteIsValid = true; SiteName = Site.SiteName; } Visitor.SiteCode = Site.SiteCode; Visitor.CreatedDate = DateTime.Now; Visitor.IPAddress = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(); Visitor.UserAgent = _accessor.HttpContext.Request.Headers["User-Agent"]; ModelState.Remove("Visitor.SiteCode"); if (!ModelState.IsValid) { FormSaved = false; FormErrors = string.Join("; ", ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage)); } _context.Visitor.Add(Visitor); await _context.SaveChangesAsync(); int userID = Visitor.VisitorID; //Store Cookie to track user ID - valid for 12 hours CookieOptions option = new CookieOptions(); option.Expires = DateTime.Now.AddHours(12); option.IsEssential = true; option.Secure = true; option.HttpOnly = true; option.SameSite = SameSiteMode.Strict; _accessor.HttpContext.Response.Cookies.Append("UserID", userID.ToString(), option); //return RedirectToPage("./Index"); return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Visitor = await _context.Visitor.FindAsync(id); if (Visitor != null) { _context.Visitor.Remove(Visitor); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnGetAsync(string site) { string ipAddress = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(); string userAgent = _accessor.HttpContext.Request.Headers["User-Agent"]; Site = (await _context.Site .FromSqlInterpolated($"EXEC SPR_VIS_SiteDetails @Site={site}") .ToListAsync()) .FirstOrDefault(); if (Site == null) { SiteIsValid = false; SiteName = "Unknown"; } else { SiteIsValid = true; SiteName = Site.SiteName; } int userID; int.TryParse(Request.Cookies["UserID"], out userID); //Cookie is valid if (userID > 0) { UserIDNotNull = true; UserIdentificationMethod = "Cookie"; } else { UserIDNotNull = false; } //If cookie valid if (UserIDNotNull == true) { Visitor = _context.Visitor .Where(x => x.VisitorID == userID) .FirstOrDefault(); //User found if (Visitor.VisitorID > 0) { UserIDValid = true; Visitor.LeaveDate = DateTime.Now; await _context.SaveChangesAsync(); Response.Cookies.Delete("UserID"); } else { UserIDValid = false; } } else //If user was invalid try searching database for record instead { Visitor = (await _context.Visitor .FromSqlInterpolated($"EXEC SPR_VIS_GetUserByIP @IPAddress={ipAddress}, @UserAgent={userAgent}, @Site={site}") .ToListAsync()) .FirstOrDefault(); if (Visitor != null) { if (Visitor.VisitorID > 0) { UserIDValid = true; userID = Visitor.VisitorID; Visitor.LeaveDate = DateTime.Now; await _context.SaveChangesAsync(); } else { UserIDValid = false; } //Single record was found if (userID > 0) { UserIDNotNull = true; UserIdentificationMethod = "Database"; } else { UserIDNotNull = false; } } else { UserIDValid = false; } } return(Page()); }