public async Task <IActionResult> Register(RegisterCustomerViewModel customer) { // If the register form is valid if (ModelState.IsValid) { var result = await _customerRegistrationService.RegisterCustomerAsync(customer); if (result.Succeeded) { // If successful, return to log in page return(RedirectToAction("index", "Home")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } } return(View()); }
/// <summary> /// Register new user /// </summary> /// <param name="parameters">Authentication parameters received from external authentication method</param> /// <param name="returnUrl">URL to which the user will return after authentication</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the result of an authentication /// </returns> protected virtual async Task <IActionResult> RegisterNewUserAsync(ExternalAuthenticationParameters parameters, string returnUrl) { //check whether the specified email has been already registered if (await _customerService.GetCustomerByEmailAsync(parameters.Email) != null) { var alreadyExistsError = string.Format(await _localizationService.GetResourceAsync("Account.AssociatedExternalAuth.EmailAlreadyExists"), !string.IsNullOrEmpty(parameters.ExternalDisplayIdentifier) ? parameters.ExternalDisplayIdentifier : parameters.ExternalIdentifier); return(ErrorAuthentication(new[] { alreadyExistsError }, returnUrl)); } //registration is approved if validation isn't required var registrationIsApproved = _customerSettings.UserRegistrationType == UserRegistrationType.Standard || (_customerSettings.UserRegistrationType == UserRegistrationType.EmailValidation && !_externalAuthenticationSettings.RequireEmailValidation); //create registration request var registrationRequest = new CustomerRegistrationRequest(await _workContext.GetCurrentCustomerAsync(), parameters.Email, parameters.Email, CommonHelper.GenerateRandomDigitCode(20), PasswordFormat.Hashed, (await _storeContext.GetCurrentStoreAsync()).Id, registrationIsApproved); //whether registration request has been completed successfully var registrationResult = await _customerRegistrationService.RegisterCustomerAsync(registrationRequest); if (!registrationResult.Success) { return(ErrorAuthentication(registrationResult.Errors, returnUrl)); } //allow to save other customer values by consuming this event await _eventPublisher.PublishAsync(new CustomerAutoRegisteredByExternalMethodEvent(await _workContext.GetCurrentCustomerAsync(), parameters)); //raise customer registered event await _eventPublisher.PublishAsync(new CustomerRegisteredEvent(await _workContext.GetCurrentCustomerAsync())); //store owner notifications if (_customerSettings.NotifyNewCustomerRegistration) { await _workflowMessageService.SendCustomerRegisteredNotificationMessageAsync(await _workContext.GetCurrentCustomerAsync(), _localizationSettings.DefaultAdminLanguageId); } //associate external account with registered user await AssociateExternalAccountWithUserAsync(await _workContext.GetCurrentCustomerAsync(), parameters); //authenticate if (registrationIsApproved) { await _workflowMessageService.SendCustomerWelcomeMessageAsync(await _workContext.GetCurrentCustomerAsync(), (await _workContext.GetWorkingLanguageAsync()).Id); //raise event await _eventPublisher.PublishAsync(new CustomerActivatedEvent(await _workContext.GetCurrentCustomerAsync())); return(await _customerRegistrationService.SignInCustomerAsync(await _workContext.GetCurrentCustomerAsync(), returnUrl, true)); } //registration is succeeded but isn't activated if (_customerSettings.UserRegistrationType == UserRegistrationType.EmailValidation) { //email validation message await _genericAttributeService.SaveAttributeAsync(await _workContext.GetCurrentCustomerAsync(), NopCustomerDefaults.AccountActivationTokenAttribute, Guid.NewGuid().ToString()); await _workflowMessageService.SendCustomerEmailValidationMessageAsync(await _workContext.GetCurrentCustomerAsync(), (await _workContext.GetWorkingLanguageAsync()).Id); return(new RedirectToRouteResult("RegisterResult", new { resultId = (int)UserRegistrationType.EmailValidation, returnUrl })); } //registration is succeeded but isn't approved by admin if (_customerSettings.UserRegistrationType == UserRegistrationType.AdminApproval) { return(new RedirectToRouteResult("RegisterResult", new { resultId = (int)UserRegistrationType.AdminApproval, returnUrl })); } return(ErrorAuthentication(new[] { "Error on registration" }, returnUrl)); }