Example #1
0
        internal async Task <IActionResult> TryCreateNewUserAccount(
            RegisterInputModel model)
        {
            UserAccount userAccount = await this._userAccountService
                                      .CreateNewLocalUserAccountAsync(
                model.Email,
                model.Password,
                model.ReturnUrl
                );

            // Send confirmation mail
            if (this._applicationOptions.RequireLocalAccountVerification)
            {
                await this._notificationService
                .SendUserAccountCreatedEmailAsync(userAccount);
            }

            if (this._applicationOptions.LoginAfterAccountCreation)
            {
                await this._authenticationService
                .SignInAsync(userAccount, model.ReturnUrl);

                return(this.RedirectToReturnUrl(
                           model.ReturnUrl, this._interaction));
            }

            return(this.View("Success",
                             this.CreateSuccessViewModel(userAccount, model.ReturnUrl)
                             ));
        }
Example #2
0
        internal async Task <RegisterViewModel> CreateViewModelAsync(
            RegisterInputModel inputModel,
            UserAccount userAccount = null)
        {
            AuthorizationRequest context = await this._interaction
                                           .GetAuthorizationContextAsync(inputModel.ReturnUrl);

            if (context == null)
            {
                return(null);
            }

            Client client = await this._clientService
                            .FindEnabledClientByIdAsync(context.ClientId);

            IEnumerable <ExternalProvider> providers = await this._clientService
                                                       .GetEnabledProvidersAsync(client);

            RegisterViewModel vm = new RegisterViewModel(inputModel)
            {
                EnableAccountRecover =
                    this._applicationOptions.EnableAccountRecovery,

                EnableLocalLogin = client != null ?
                                   client.EnableLocalLogin :
                                   false && this._applicationOptions.EnableAccountLogin,

                ExternalProviders = providers.ToArray(),

                ExternalProviderHints = userAccount?.Accounts?
                                        .Select(c => c.Provider)
            };

            return(vm);
        }
 public RegisterViewModel(RegisterInputModel inputModel)
 {
     this.Email           = inputModel.Email;
     this.Password        = inputModel.Password;
     this.PasswordConfirm = inputModel.PasswordConfirm;
     this.ReturnUrl       = inputModel.ReturnUrl;
 }
Example #4
0
        public async Task <IActionResult> Index(RegisterInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(await this.CreateViewModelAsync(model)));
            }

            string email = model.Email.ToLower();

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

            // If user dont exists create a new one
            if (userAccount == null)
            {
                return(await this.TryCreateNewUserAccount(model));
            }
            // User is just disabled by whatever reason
            else if (!userAccount.IsLoginAllowed)
            {
                this.ModelState.AddModelError(
                    this._localizer[ErrorMessages.AccountIsDesabled]);
            }
            // If user has a password then its a local account
            else if (userAccount.HasPassword())
            {
                // User has to follow a link in confirmation mail
                if (this._applicationOptions.RequireLocalAccountVerification &&
                    !userAccount.IsEmailVerified)
                {
                    this.ModelState.AddModelError(
                        this._localizer[ErrorMessages.ConfirmAccount]);

                    // TODO: show link for resent confirmation link
                }

                // If user has a password then its a local account
                this.ModelState.AddModelError(
                    this._localizer[ErrorMessages.AccountAlreadyExists]);
            }
            else
            {
                // External account with same email
                return(await this.TryMergeWithExistingUserAccount(
                           userAccount,
                           model
                           ));
            }

            return(this.View(
                       await this.CreateViewModelAsync(model, userAccount)
                       ));
        }
Example #5
0
        internal async Task <IActionResult> TryMergeWithExistingUserAccount(
            UserAccount userAccount,
            RegisterInputModel model)
        {
            // Merge accounts without user consent
            if (this._applicationOptions.AutomaticAccountMerge)
            {
                await this._userAccountService
                .AddLocalCredentialsAsync(userAccount, model.Password);

                if (this._applicationOptions.LoginAfterAccountCreation)
                {
                    await this._authenticationService
                    .SignInAsync(userAccount, model.ReturnUrl);
                }
                else
                {
                    return(this.View("Success",
                                     this.CreateSuccessViewModel(
                                         userAccount, model.ReturnUrl)
                                     ));
                }
            }
            // Ask user if he wants to merge accounts
            else
            {
                throw new NotImplementedException();
            }

            // Return list of external account providers as hint
            RegisterViewModel vm = new RegisterViewModel(model)
            {
                ExternalProviderHints = userAccount.Accounts
                                        .Select(s => s.Provider).ToArray()
            };

            return(View(vm));
        }