Ejemplo n.º 1
0
        public async Task <IActionResult> Create(UserCreateMdl model)
        {
            if (model.SelectedPerson == "0")
            {
                ModelState.AddModelError("", "A person must be selected.");
            }

            if (string.IsNullOrEmpty(model.SelectedRoles))
            {
                ModelState.AddModelError("", "At least one role must be selected.");
            }

            if (!ModelState.IsValid)
            {
                model.LoadPersonList(_dbContext, model.SelectedPerson);
                model.LoadRoleList(_dbContext, model.SelectedRoles);
                return(View(model));
            }

            var user = new IdentityUser {
                UserName = model.Username
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var roleList = model.SelectedRoles.Split(new char[] { ';' });
                result = await _userManager.AddToRolesAsync(user, roleList);

                _dbContext.IdentityPerson.Add(new IdentityPerson {
                    IdentityId = user.Id, PersonId = Int32.Parse(model.SelectedPerson)
                });
                _dbContext.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                model.LoadPersonList(_dbContext, model.SelectedPerson);
                model.LoadRoleList(_dbContext, model.SelectedRoles);
                return(View(model));
            }
        }
Ejemplo n.º 2
0
        public IActionResult Create()
        {
            var model = new UserCreateMdl();

            model.LoadPersonList(_dbContext);
            model.LoadRoleList(_dbContext, null);

            return(View(model));
        }