예제 #1
0
        //[Authorize(Roles = Role.Administrator)]
        //[Authorize(Roles = Role.Doctor)]
        public IActionResult Register()
        {
            var model = new RegisterViewModel
            {
                RoleItems = _roleManager.Roles.Select(iR => new SelectListItem
                {
                    Text  = iR.Name,
                    Value = iR.Name
                })
            };

            return(View(model));
        }
예제 #2
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName    = model.FirstName + model.LastName,
                    Name        = model.FirstName + " " + model.LastName,
                    Email       = model.Email,
                    gender      = model.gender,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (!await _roleManager.RoleExistsAsync(Role.User))
                {
                    await _roleManager.CreateAsync(new IdentityRole(Role.User));
                }

                if (!await _roleManager.RoleExistsAsync(Role.Administrator))
                {
                    await _roleManager.CreateAsync(new IdentityRole(Role.Administrator));
                }

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    TempData["UserID"] = user.Id;
                    return(RedirectToAction("Index", "Home"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(String.Empty, error.Description);
                }
            }
            return(View(model));
        }