Exemplo n.º 1
0
        public async Task <IActionResult> AddUser(UtilizadorViewModel model)
        {
            if (ModelState.IsValid)
            {
                Utilizador user = new Utilizador
                {
                    Name     = model.Name,
                    UserName = model.UserName,
                    Email    = model.Email
                };


                IdentityResult result = await userManager.CreateAsync(user, model.Password);


                if (result.Succeeded)
                {
                    ApplicationRole applicationRole = await roleManager.FindByIdAsync(model.ApplicationRoleId);

                    if (applicationRole != null)
                    {
                        IdentityResult roleResult = await userManager.AddToRoleAsync(user, applicationRole.Name);

                        if (roleResult.Succeeded)
                        {
                            return(RedirectToAction("Index"));
                        }
                    }
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public IActionResult AddUser()
        {
            UtilizadorViewModel model = new UtilizadorViewModel();

            model.ApplicationRoles = roleManager.Roles.Select(r => new SelectListItem
            {
                Text  = r.Name,
                Value = r.Id
            }).ToList();
            return(PartialView("_AddUser", model));
        }