public async Task <ActionResult> AccountGridViewPartialAddNewAsync(Competition.Web.Models.AccountEditModel item) { //转换users的模型 List <AccountEditModel> model = new List <AccountEditModel>(); foreach (var applicationUser in UserManager.Users) { model.Add(new AccountEditModel(applicationUser, RoleManager.Roles.ToList())); } if (ModelState.IsValid) { try { var user = new ApplicationUser { UserName = item.UserName, StaffId = item.StaffId, RealName = item.RealName }; var result = await UserManager.CreateAsync(user, string.IsNullOrEmpty(item.PassWord)?item.PassWord : "******"); if (result.Succeeded) { foreach (var roleId in item.RoleIds) { UserManager.AddToRole(user.Id, RoleManager.FindById(roleId).Name); } return(PartialView("_AccountGridViewPartial", model.ToList())); } else { ViewData["EditError"] = Infrastructure.MyHandler.IdentityResultErrorsToString(result); } } catch (Exception e) { ViewData["EditError"] = e.Message; } } else { ViewData["EditError"] = "Please, correct all errors."; } return(PartialView("_AccountGridViewPartial", model.ToList())); }
public async Task <ActionResult> AccountGridViewPartialUpdateAsync(Competition.Web.Models.AccountEditModel item) { List <AccountEditModel> model = new List <AccountEditModel>(); foreach (var applicationUser in UserManager.Users) { model.Add(new AccountEditModel(applicationUser, RoleManager.Roles.ToList())); } ApplicationUser user = await UserManager.FindByIdAsync(item.Id); if (ModelState.IsValid) { try { if (user != null) { if (!string.IsNullOrEmpty(item.PassWord)) { //验证密码是否满足要求 IdentityResult validPass = await UserManager.PasswordValidator.ValidateAsync(item.PassWord); if (validPass.Succeeded) { user.PasswordHash = UserManager.PasswordHasher.HashPassword(item.PassWord); } else { ViewData["EditError"] = validPass.Errors; } } user.StaffId = item.StaffId; user.RealName = item.RealName; user.UserName = item.UserName; IdentityResult result = await UserManager.UpdateAsync(user); if (result.Succeeded) { IEnumerable <string> orginroles = user.Roles.Select(r => r.RoleId); IEnumerable <string> unchangedroles = item.RoleIds.Where(x => orginroles.Any(y => y == x)); IEnumerable <string> newrole = item.RoleIds.Except(unchangedroles); IEnumerable <string> deleterole = orginroles.Except(unchangedroles); foreach (string roleId in newrole) { result = await UserManager.AddToRoleAsync(user.Id, RoleManager.FindById(roleId).Name); if (!result.Succeeded) { ViewData["EditError"] = Infrastructure.MyHandler.IdentityResultErrorsToString(result); } } foreach (string roleId in deleterole) { result = await UserManager.RemoveFromRoleAsync(user.Id, RoleManager.FindById(roleId).Name); if (!result.Succeeded) { ViewData["EditError"] = Infrastructure.MyHandler.IdentityResultErrorsToString(result); } } return(PartialView("_AccountGridViewPartial", model.ToList())); } else { ViewData["EditError"] = Infrastructure.MyHandler.IdentityResultErrorsToString(result); } //if (validPass == null || validPass.Succeeded) //{ // IdentityResult result = await UserManager.UpdateAsync(user); // if (result.Succeeded) // { // return RedirectToAction("Index"); // } // AddErrors(result); //} } } catch (Exception e) { ViewData["EditError"] = e.Message; } } else { ViewData["EditError"] = "Please, correct all errors."; } return(PartialView("_AccountGridViewPartial", model.ToList())); }