private async Task <IActionResult> TryCreateNewUserAccount(
            UserAccount userAccount,
            RegisterInputModel model)
        {
            userAccount = await _userAccountService.CreateNewLocalUserAccountAsync(
                model.Email, model.Password, model.ReturnUrl);

            // Send confirmation mail
            if (_applicationOptions.RequireLocalAccountVerification)
            {
                await SendUserAccountCreatedAsync(userAccount);
            }

            if (_applicationOptions.LoginAfterAccountCreation)
            {
                await HttpContext.Authentication.SignInAsync(userAccount, null);

                if (model.ReturnUrl != null && _interaction.IsValidReturnUrl(model.ReturnUrl))
                {
                    return(Redirect(model.ReturnUrl));
                }
            }

            return(await this.RedirectToSuccessAsync(userAccount, model.ReturnUrl));
        }
        private async Task <IActionResult> TryMergeWithExistingUserAccount(
            UserAccount userAccount,
            RegisterInputModel model)
        {
            // Merge accounts without user consent
            if (_applicationOptions.AutomaticAccountMerge)
            {
                await _userAccountService.AddLocalCredentialsAsync(userAccount, model.Password);

                if (_applicationOptions.LoginAfterAccountCreation)
                {
                    await HttpContext.Authentication.SignInAsync(userAccount, null);

                    if (model.ReturnUrl != null && _interaction.IsValidReturnUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }
                }
                else
                {
                    return(await this.RedirectToSuccessAsync(userAccount, model.ReturnUrl));
                }
            }
            // Ask user if he wants to merge accounts
            else
            {
                throw new NotImplementedException();
            }

            // Return list of external account providers as hint
            var vm = new RegisterViewModel(model);

            vm.HintExternalAccounts = userAccount.Accounts.Select(s => s.Provider).ToArray();
            return(View(vm));
        }
        public async Task <IActionResult> Index(RegisterInputModel model)
        {
            if (ModelState.IsValid)
            {
                var email = model.Email.ToLower();

                // Check if user with same email exists
                var userAccount = await _userAccountService.LoadByEmailWithExternalAsync(email);

                // If user dont exists create a new one
                if (userAccount == null)
                {
                    return(await this.TryCreateNewUserAccount(userAccount, model));
                }
                // User is just disabled by whatever reason
                else if (!userAccount.IsLoginAllowed)
                {
                    ModelState.AddModelError("", "Your user account has be disabled");
                }
                // If user has a password then its a local account
                else if (userAccount.HasPassword())
                {
                    // User has to follow a link in confirmation mail
                    if (_applicationOptions.RequireLocalAccountVerification &&
                        !userAccount.IsEmailVerified)
                    {
                        ModelState.AddModelError("", "Please confirm your email account");

                        // TODO: show link for resent confirmation link
                    }

                    // If user has a password then its a local account
                    ModelState.AddModelError("", "User already exists");
                }
                // External account with same email
                else
                {
                    return(await this.TryMergeWithExistingUserAccount(userAccount, model));
                }
            }

            return(View(new RegisterViewModel(model)));
        }
Exemple #4
0
 public RegisterViewModel(RegisterInputModel other)
 {
     this.Email           = other.Email;
     this.Password        = other.Password;
     this.PasswordConfirm = other.PasswordConfirm;
 }