public async Task <IActionResult> Register([FromBody] UserEditViewModel user) { if (!(await _authorizationService.AuthorizeAsync(User, (user.Roles, new string[] { }), Authorization.Policies.AssignAllowedRolesPolicy)).Succeeded) { return(new ChallengeResult()); } if (ModelState.IsValid) { if (user == null) { return(BadRequest($"{nameof(user)} cannot be null")); } User appUser = _mapper.Map <User>(user); (bool Succeeded, string[] Errors)result = await _accountManager.CreateUserAsync(appUser, user.Roles, user.NewPassword); if (result.Succeeded) { UserViewModel userVM = await GetUserViewModelHelper(appUser.Id); return(CreatedAtAction(GetUserByIdActionName, new { id = userVM.Id }, userVM)); } AddError(result.Errors); } return(BadRequest(ModelState)); }
private async Task <User> CreateUserAsync(IIdentityManager accountManager, string jobTitle, string userName, string password, string fullName, string email, string phoneNumber, string[] roles) { User User = new User { JobTitle = jobTitle, UserName = userName, FullName = fullName, Email = email, PhoneNumber = phoneNumber, EmailConfirmed = true, IsEnabled = true }; (bool Succeeded, string[] Errors) = await accountManager.CreateUserAsync(User, roles, password); if (!Succeeded) { throw new Exception($"Seeding \"{userName}\" user failed. Errors: {string.Join(Environment.NewLine, Errors)}"); } return(User); }
public async Task <ActionResult> Create(AddEditUserViewModel model) { if (ModelState.IsValid) { var aspnetuser = new AspNetUser() { UserName = model.UserName, FirstName = model.FirstName, MiddleInitial = model.MiddleInitial, LastName = model.LastName, Address1 = model.Address1, Address2 = model.Address2, City = model.City, Province = model.Province, District = model.District, PostalCode = model.PostalCode, IsUserActive = model.IsUserActive, Email = model.Email, EmailConfirmed = true, //set PasswordHash = new PasswordHasher().HashPassword(model.Password), PhoneNumber = model.PhoneNumber, PhoneNumberConfirmed = false, //set AlternatePhoneNumber = model.AlternatePhoneNumber.ToString(), TwoFactorEnabled = true, //set LockoutEnabled = false, AccessFailedCount = 0, CreatedDate = DateTime.Now, LastUpdatedDate = DateTime.Now, LastUpdatedByUser = await _identityManager.GetLoggedInUserIdAsync() }; bool isUserCreated = await _identityManager.CreateUserAsync(aspnetuser); return(RedirectToAction(nameof(SearchUsers))); } return(View(model)); }