Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("ApplicationGroupId,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] ApplicationUser applicationUser)
        {
            ApplicationUser appUser = new ApplicationUser
            {
                Email              = applicationUser.Email,
                UserName           = applicationUser.Email,
                ApplicationGroupId = applicationUser.ApplicationGroupId
            };
            await _userManager.CreateAsync(appUser, applicationUser.PasswordHash);

            await _role.ChangeRole(appUser, appUser.ApplicationGroupId);

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                List <ApplicationUser> listuser = _context.ApplicationUser.ToList();
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                if (listuser.Count() == 0)
                {
                    user.ApplicationGroupId = 1;
                }
                else
                {
                    user.ApplicationGroupId = 2;
                }
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _role.ChangeRole(user, user.ApplicationGroupId);

                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, 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);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }