/// <summary> /// Add User /// </summary> private string AddUserToUserManager(AspNetUser user, UserManagement model) { var result = UserManager.Create(user, model.Password); if (result.Succeeded) { var addUserToRoleResult = UserManager.AddToRole(user.Id, model.UserRole); if (!addUserToRoleResult.Succeeded) { throw new InvalidOperationException(string.Format("Failed to add user to role {0}", model.UserRole)); } } return result.Errors.FirstOrDefault(); }
public ActionResult CreateUser(UserManagement model) { if (model == null) throw new InvalidOperationException("User Does not exists!"); var domainKeyClaim = ClaimHelper.GetClaimToString(CaresUserClaims.UserDomainKey); if (domainKeyClaim == null) { throw new InvalidOperationException("Domain-Key claim not found!"); } var domainkey = System.Convert.ToInt64(domainKeyClaim.Value); // Creating employee //DomainModels.Employee emp = employeeService.CreateEmployeeWithUser(model.UserEmail); var user = new AspNetUser { PhoneNumber = model.PhoneNumber, UserName = model.UserEmail, Email = model.UserEmail, UserDomainKey = domainkey, FirstName = model.FirstName, LastName = model.LastName }; var status = AddUserToUserManager(user, model); if (status == null) return RedirectToAction("Index"); var roles = RoleManager.Roles.Where(role => role.Name != "SystemAdministrator").ToList(); var allUsers = GetAllUsers().ToList(); //var allEmployees = employeeService.GetAllForUser(allUsers.Select(usr => usr.EmployeeId).ToList()).Select(employee => employee.CreateFrom()); ViewBag.UserRoles = roles; //ViewBag.Employees = allEmployees; ViewBag.UserError = status; return View(new UserManagement()); }