// GET: User/Create
        public async Task <IActionResult> Create()
        {
            UserCreatetVm model = new UserCreatetVm();

            model.ApplicationRoles = await _role.Roles.Select(r => new SelectListItem
            {
                Text  = r.Name,
                Value = r.Id
            }).ToListAsync();

            return(View(model));
        }
Example #2
0
        public async Task <IActionResult> Register(UserCreatetVm model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                User user = new User
                {
                    Name     = model.Name,
                    UserName = model.Email,
                    Email    = model.Email,
                    Status   = ContactStatus.Submitted
                };

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

                if (result.Succeeded)
                {
                    UserRole applicationRole = await _roleManager.FindByNameAsync(Constants.Student);

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

                        if (roleResult.Succeeded)
                        {
                            _logger.LogInformation("User created a new account with password.");

                            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                            var callbackUrl = Url.EmailConfirmationLink(user.Id.ToString(), code, Request.Scheme);
                            await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                            await _signInManager.SignInAsync(user, isPersistent : false);

                            _logger.LogInformation("User created a new account with password.");
                            return(RedirectToLocal(returnUrl));
                        }
                    }
                    AddErrors(result);
                }
            }
            return(View(model));
        }
        public async Task <IActionResult> Create(UserCreatetVm model)
        {
            if (ModelState.IsValid)
            {
                User user = new User
                {
                    Name        = model.Name,
                    UserName    = model.Email,
                    Email       = model.Email,
                    PhoneNumber = model.PhoneNumber,
                    City        = model.City,
                    State       = model.State,
                    Status      = ContactStatus.Submitted,
                    Address     = model.Address,
                    Zip         = model.Zip
                };

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

                if (result.Succeeded)
                {
                    UserRole applicationRole = await _role.FindByIdAsync(model.ApplicationRoleId);

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

                        if (roleResult.Succeeded)
                        {
                            return(RedirectToAction("Index"));
                        }
                    }
                }
            }
            return(View(model));
        }