// 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(Role).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RoleExists(Role.Id)) { 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() { if (!ModelState.IsValid) { return(Page()); } _context.Roles.Add(Role); await _context.SaveChangesAsync(); 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[] selectedRoles) { if (!ModelState.IsValid) { return(Page()); } var neoUser = new User(); if (selectedRoles != null) { neoUser.RoleUsers = new List <RoleUser>(); foreach (var role in selectedRoles) { var roleToAdd = new RoleUser { RoleId = int.Parse(role) }; neoUser.RoleUsers.Add(roleToAdd); } } if (await TryUpdateModelAsync <User>( neoUser, "user", u => u.Name, u => u.LastName, u => u.Email, u => u.RegistrationDate, u => u.Password, u => u.ValidatePassword )) { if (neoUser.Password == neoUser.ValidatePassword) { ErrorPasswordMessage = ""; neoUser.Password = encrypt.EncSHA256((User.Password)); neoUser.RegistrationDate = DateTime.Now; _context.Users.Add(neoUser); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } else { ErrorPasswordMessage = "(X) Las contraseñas deben ser iguales."; } } PopulateAssignedRoleData(_context, neoUser); return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } User = await _context.Users.FindAsync(id); if (User != null) { _context.Users.Remove(User); await _context.SaveChangesAsync(); } 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(int?id, string[] selectedRoles) { if (!ModelState.IsValid) { return(Page()); } if (id == null) { return(NotFound()); } var currentUser = await _context.Users .Include(u => u.RoleUsers) .ThenInclude(u => u.Role) .FirstOrDefaultAsync(u => u.Id == id); /*var currentUser = await _context.Users.FindAsync(id);*/ if (currentUser == null) { return(NotFound()); } if (await TryUpdateModelAsync <User>( currentUser, "user", u => u.Name, u => u.Email, u => u.LastName, u => u.Password, u => u.ValidatePassword )) { if (currentUser.Password == currentUser.ValidatePassword) { ErrorPasswordMessage = ""; currentUser.Password = encrypt.EncSHA256((User.Password)); UpdateUserRoles(_context, selectedRoles, currentUser); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(User.Id)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); } else { ErrorPasswordMessage = "(X) Las contraseñas deben ser iguales."; } } UpdateUserRoles(_context, selectedRoles, currentUser); PopulateAssignedRoleData(_context, currentUser); return(Page()); }