public async Task<ActionResult> Create(AdminUserCreateModel model) { var user = new User(); if (!TryUpdateModel2(user, model)) { return Template(model.CreateTemplate(ControllerContext)); } var db = MaintDbContext; var loginName = model.LoginName.Trim(); if (await db.Users.AnyAsync(x => x.LoginName == loginName)) { var errorMessage = string.Format(MaintCultureTextResources.ValidationDumplicate, MaintCultureTextResources.AdminUserLoginName, loginName); ModelState.AddModelError("LoginName", string.Format(errorMessage, loginName)); return Template(model.CreateTemplate(ControllerContext)); } db.Users.Add(user); await db.SaveChangesAsync(); await SetFlashAsync(new FlashMessage { Text = MaintCultureTextResources.SuccessToSave, MessageType = FlashMessageType.Success, }); return Create(); }
public ActionResult AddUser() { var roles = roleManager.Roles.ToList(); AdminUserCreateModel model = new AdminUserCreateModel { Roles = roles }; return(View(model)); }
public async Task <int> Create(AdminUserCreateModel m, int creator) { if (m == null) { throw new ArgumentNullException(nameof(m)); } if (string.IsNullOrWhiteSpace(m.UserName)) { throw new HopexException(_localizer["用户名不能为空"]); } if (string.IsNullOrWhiteSpace(m.Password)) { throw new HopexException(_localizer["密码不能为空"]); } if (m.Password.Contains(' ')) { throw new HopexException(_localizer["密码不能包含空格"]); } if (!PasswordStrongCheck(m.Password)) { throw new HopexException(_localizer["密码强度不够(至少6位数)"]); } if ((await _adminUserRepository.CountAsync(x => x.UserName == m.UserName && x.IsValid && x.IsDeleted == false)) != 0) { throw new HopexException(_localizer["用户名已存在"]); } var(salt, passwordHash) = HashUtil.PasswordHash(m.Password); var user = new AdminUser() { LastModifiedTime = DateTime.Now, LastModifier = creator, IsValid = true, UserName = m.UserName.Trim(), Salt = salt, PasswordHash = passwordHash, IsAdmin = m.IsAdmin }; int id = await _adminUserRepository.InsertAsync(user); return(id); }
public ActionResult AddUser(AdminUserCreateModel model) { if (!ModelState.IsValid) { TempData["message"] = "정보를 정확하게 입력해주세요."; } var user = new AppUser { UserName = model.UserName }; if (model.SelectRoles != null) { foreach (var roleName in model.SelectRoles) { var Role = roleManager.FindByName(roleName); AppUserRole userRole = new AppUserRole { RoleId = Role.Id, UserId = user.Id }; user.Roles.Add(userRole); } IdentityResult result = userManager.Create(user, model.Password); if (result.Succeeded) { TempData["message"] = "관리자 계정이 생성되었습니다."; return(RedirectToAction("Index")); } else { AddErrorsFromResult(result); } } else { TempData["message"] = "역할을 선택해주세요."; } model.Roles = roleManager.Roles.ToList(); return(View(model)); }
public ActionResult Create(AdminUserCreateModel model) { if (!ModelState.IsValid) { return(Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) })); } bool exists = AdminUserService.GetByPhoneNum(model.PhoneNum) != null; if (exists) { return(Json(new AjaxResult { Status = "error", ErrorMsg = "手机号已经存在" })); } long adminUserId = AdminUserService.AddAdminUser(model.Name, model.PhoneNum, model.Password, model.Email, model.CityId == 0 ? null : model.CityId); RoleService.AddRoleIds(adminUserId, model.roleIds); return(Json(new AjaxResult { Status = "ok" })); }
public ActionResult Create() { var model = new AdminUserCreateModel(); return Template(model.CreateTemplate(ControllerContext)); }