コード例 #1
0
        public ActionResult AddUser(AddUserVM vm)
        {
            var roleManager = new RoleManager <AppRole>(new RoleStore <AppRole>(new EntityRepo()));
            var userManager = new UserManager <AppUser>(new UserStore <AppUser>(new EntityRepo()));

            if (vm.Password != vm.ConfirmPassword)
            {
                ModelState.AddModelError("", "The password and confirm password fields do not match.");
            }


            if (!ModelState.IsValid)
            {
                vm.SetRoles(roleManager);
                return(View(vm));
            }
            else
            {
                var selectedRole = roleManager.FindById(vm.SelectedRoleId).Name;
                var user         = new AppUser
                {
                    FirstName = vm.FirstName,
                    LastName  = vm.LastName,
                    Email     = vm.Email,
                    UserName  = vm.Email
                };
                userManager.Create(user, vm.Password);
                userManager.AddToRole(user.Id, selectedRole);
                return(RedirectToAction("Users"));
            }
        }
コード例 #2
0
        public ActionResult AddUser()
        {
            var vm          = new AddUserVM();
            var roleManager = new RoleManager <AppRole>(new RoleStore <AppRole>(new EntityRepo()));

            vm.SetRoles(roleManager);
            return(View(vm));
        }