// To protect from overposting attacks, please 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()); } try { //GRAB ACCOUNTS IList <Account> Accounts = _context.Accounts.ToList(); foreach (var account in Accounts) { //ERROR IF USERNAME NOT UNIQUE if (account.Username == Account.Username) { ErrorMsg = "Username already exists"; return(Page()); } } //INITIALISE SCORE Account.Score = 0; Account.Password = BCrypt.Net.BCrypt.HashPassword(Account.Password); //HASH PASSWORD WITH BCRYPT _context.Accounts.Add(Account); await _context.SaveChangesAsync(); //UPDATE DATABASE var userClaims = new List <Claim>() { //CREATE COOKIE new Claim(ClaimTypes.Name, Account.Username), new Claim("Triviapp", "This is a user") }; var userIdentity = new ClaimsIdentity(userClaims, "User Identity"); var userPrincipal = new ClaimsPrincipal(userIdentity); await HttpContext.SignInAsync(userPrincipal); return(RedirectToPage("/Quizzes/Browse")); } catch { //ERROR ACCESSING DATABASE ErrorMsg = "An error occurred, please try again later"; return(Page()); } }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { //Quiz data validation failure if (!ModelState.IsValid) { return(Page()); } //BINDS CURRENT DATE Quiz.DateAdded = DateTime.Today; //GETS ACCOUNT ID FROM USERNAME string Username = HttpContext.User.Identity.Name.ToString(); var account = _context.Accounts.Where(a => a.Username == Username).FirstOrDefault(); //BINDS ACCOUNT ID TO QUIZ Quiz.AccountID = account.ID; _context.Quizzes.Add(Quiz); await _context.SaveChangesAsync(); return(RedirectToPage("/Quizzes/Browse")); }