public IActionResult OnPost() { if (!IdentityUtility.Authority?.UserManager?.IsUserAllowed(User) ?? false) { throw Authority.New_UnauthorizedAccessException; } var roles = _roleManager.Roles.Select(x => x.Name).ToArray(); ViewData["Roles"] = roles; var password = Request.Form["password"].ToString(); if (password.IsNullOrEmpty()) { ModelState.AddModelError("password", "Password can not be null."); return(Page()); } if (ModelState.IsValid) { var user = new IdentityUser(Input.UserName); var userResult = _userManager.CreateAsync(user, password).Result; if (userResult.Succeeded) { user.Then(_ => { _.PhoneNumberConfirmed = Input.PhoneNumberConfirmed; _.EmailConfirmed = Input.EmailConfirmed; _.AccessFailedCount = Input.AccessFailedCount; }); var results = new[] { _userManager.SetUserNameAsync(user, Input.UserName).Result, _userManager.SetEmailAsync(user, Input.Email).Result, _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber).Result, _userManager.SetLockoutEndDateAsync(user, Input.LockoutEnd).Result, Input.LockoutEnabled.For(_ => { if (_) { return(_userManager.SetLockoutEndDateAsync(user, Input.LockoutEnd).Result); } else { return(IdentityResult.Success); } }), _userManager.UpdateAsync(user).Result, _userManager.AddToRolesAsync(user, Request.Form["roles"]).Result, }; if (results.All(x => x.Succeeded)) { _logger.LogInformation($"Create role {user.Id}({user.UserName}) succeeded."); return(Redirect("Index")); } else { ModelState.AddModelError(string.Empty, results.SelectMany(x => x.Errors).Select(x => x.Description).Join("<br/>")); return(Page()); } } else { ModelState.AddModelError(string.Empty, userResult.Errors.Select(x => x.Description).Join("<br/>")); return(Page()); } } return(Page()); }