public ActionResult Edit(string id, ApplicationUserEdit newItem) { // Validate the input if (!ModelState.IsValid) { // Display the edit form again return(RedirectToAction("edit", new { id = newItem.Id })); } if (id != newItem.Id) { // This appears to be data tampering, so redirect the user away return(RedirectToAction("index")); } // Attempt to do the upate var editedItem = m.ApplicationUserEdit(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.Id })); } else { // Show the details view, which will have the updated data return(RedirectToAction("details", new { id = newItem.Id })); } }
// Edit User Claims - For Now Only Roles public ApplicationUserDetail ApplicationUserEdit(ApplicationUserEdit newItem) { var result = new IdentityResult(); // Attempt to fetch the object var o = UserManager.FindById(newItem.Id); if (o == null) { return(null); } var userIdentity = UserManager.CreateIdentity(o, DefaultAuthenticationTypes.ApplicationCookie) as ClaimsIdentity; var claimsPrincipal = new ClaimsPrincipal(userIdentity); var userAccount = new RequestUser(claimsPrincipal); // Remove all roles foreach (var role in userAccount.RoleClaims) { result = UserManager.RemoveClaimAsync(o.Id, new Claim(ClaimTypes.Role, role)).Result; } // If successful removal, Add Roles if (result.Succeeded) { foreach (var newRole in newItem.Roles) { result = UserManager.AddClaimAsync(o.Id, new Claim(ClaimTypes.Role, newRole)).Result; } if (result.Succeeded) { return(mapper.Map <ApplicationUserDetail>(newItem)); } } return(null); }
public ActionResult Edit(string id, ApplicationUserEdit newItem) { // Validate the input if (!ModelState.IsValid) { // Display the edit form again return RedirectToAction("edit", new { id = newItem.Id }); } if (id != newItem.Id) { // This appears to be data tampering, so redirect the user away return RedirectToAction("index"); } // Attempt to do the upate var editedItem = m.ApplicationUserEdit(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return RedirectToAction("edit", new { id = newItem.Id }); } else { // Show the details view, which will have the updated data return RedirectToAction("details", new { id = newItem.Id }); } }
// Edit User Claims - For Now Only Roles public ApplicationUserDetail ApplicationUserEdit(ApplicationUserEdit newItem) { var result = new IdentityResult(); // Attempt to fetch the object var o = UserManager.FindById(newItem.Id); if (o == null) { return null; } var userIdentity = UserManager.CreateIdentity(o, DefaultAuthenticationTypes.ApplicationCookie) as ClaimsIdentity; var claimsPrincipal = new ClaimsPrincipal(userIdentity); var userAccount = new UserAccount(claimsPrincipal); // Remove all roles foreach (var role in userAccount.RoleClaims) { result = UserManager.RemoveClaimAsync(o.Id, new Claim(ClaimTypes.Role, role)).Result; } // If successful removal, Add Roles if (result.Succeeded) { foreach (var newRole in newItem.Roles) { result = UserManager.AddClaimAsync(o.Id, new Claim(ClaimTypes.Role, newRole)).Result; } if (result.Succeeded) { return Mapper.Map<ApplicationUserDetail>(newItem); } } return null; }