public ActionResult CreateUser(UserInfoView item) { try { var user = new ApplicationUser { Id = Guid.NewGuid().ToString(), UserName = item.LoginName, EmailConfirmed = false, FullName = item.FullName, TwoFactorEnabled = true, }; foreach (var roleName in item.RoleIds.AsList()) { user.Roles.Add(new IdentityUserRole() { RoleId = roleName, UserId = user.Id }); } if (string.IsNullOrWhiteSpace(item.Password)) { return Json(new ActionResultStatus(10, "密码不能为空!"), JsonRequestBehavior.AllowGet); } UserManager.Create(user, item.Password); return Json(new ActionResultStatus(), JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new ActionResultStatus(ex), JsonRequestBehavior.AllowGet); } }
public ActionResult UpdateUser(UserInfoView infoView) { try { var user = UserManager.FindByName(infoView.LoginName); if (user == null) { return Json(new ActionResultStatus(10, "用户不存在!"), JsonRequestBehavior.AllowGet); } user.UserName = infoView.LoginName; user.FullName = infoView.FullName; user.Roles.Clear(); foreach (var roleId in infoView.RoleIds) { user.Roles.Add(new IdentityUserRole { RoleId = roleId, UserId = user.Id }); } UserManager.Update(user); if (!string.IsNullOrWhiteSpace(infoView.Password)) { var token = UserManager.GeneratePasswordResetToken(infoView.Id); var result = UserManager.ResetPassword(infoView.Id, token, infoView.Password); return Json( !result.Succeeded ? new ActionResultStatus(10, result.Errors.First()) : new ActionResultStatus(), JsonRequestBehavior.AllowGet); } return Json(new ActionResultStatus(), JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new ActionResultStatus(ex), JsonRequestBehavior.AllowGet); } }
public ActionResult GetUserList(int pageIndex, int pageSize = 20) { var roles = RoleManager.Roles.ToList(); var users = UserManager.Users.ToList(); var list = new List<UserInfoView>(); users.ForEach( u => { var user = new UserInfoView { FullName = u.FullName, Id = u.Id, LoginName = u.UserName, RoleIds = u.Roles.Select(r => r.RoleId).ToList(), RoleNames = string.Join( ",", roles.Where(r => u.Roles.Select(ur => ur.RoleId).Contains(r.Id)).Select(r => r.Name).ToList()) }; list.Add(user); }); var pager = new Pager<UserInfoView> { PageIndex = pageIndex, PageSize = pageSize, Datas = list, Total = list.Count }; pager.GetShowIndex(); return Json(pager, JsonRequestBehavior.AllowGet); }