public async Task <ActionResult> Register(Register formModel) { var user = new VirtoCommercePlatformCoreSecurityApplicationUserExtended { Email = formModel.Email, Password = formModel.Password, UserName = formModel.Email, UserType = "Customer", StoreId = WorkContext.CurrentStore.Id, }; //Register user in VC Platform (create security account) var result = await _commerceCoreApi.StorefrontSecurityCreateAsync(user); if (result.Succeeded == true) { //Load newly created account from API var storefrontUser = await _commerceCoreApi.StorefrontSecurityGetUserByNameAsync(user.UserName); //Next need create corresponding Customer contact in VC Customers (CRM) module //Contacts and account has the same Id. var customer = formModel.ToWebModel(); customer.Id = storefrontUser.Id; customer.UserId = storefrontUser.Id; customer.UserName = storefrontUser.UserName; customer.IsRegisteredUser = true; customer.AllowedStores = storefrontUser.AllowedStores; await _customerService.CreateCustomerAsync(customer); await _commerceCoreApi.StorefrontSecurityPasswordSignInAsync(storefrontUser.UserName, formModel.Password); var identity = CreateClaimsIdentity(customer); _authenticationManager.SignIn(identity); //Publish user login event await _userLoginEventPublisher.PublishAsync(new UserLoginEvent(WorkContext, WorkContext.CurrentCustomer, customer)); return(StoreFrontRedirect("~/account")); } else { ModelState.AddModelError("form", result.Errors.First()); } return(View("customers/register", WorkContext)); }
public async Task <ActionResult> Register(Register formModel) { var anonymousShoppingCart = WorkContext.CurrentCart; var user = new VirtoCommercePlatformCoreSecurityApplicationUserExtended { Email = formModel.Email, Password = formModel.Password, UserName = formModel.Email, }; var result = await _commerceCoreApi.StorefrontSecurityCreateAsync(user); if (result.Succeeded == true) { user = await _commerceCoreApi.StorefrontSecurityGetUserByNameAsync(user.UserName); var contact = new VirtoCommerceCustomerModuleWebModelContact { Id = user.Id, Emails = new List <string> { formModel.Email }, FullName = string.Join(" ", formModel.FirstName, formModel.LastName), }; if (string.IsNullOrEmpty(contact.FullName)) { contact.FullName = formModel.Email; } contact = await _customerApi.CustomerModuleCreateContactAsync(contact); await _commerceCoreApi.StorefrontSecurityPasswordSignInAsync(formModel.Email, formModel.Password); var identity = CreateClaimsIdentity(formModel.Email); _authenticationManager.SignIn(identity); await MergeShoppingCartsAsync(formModel.Email, anonymousShoppingCart); return(StoreFrontRedirect("~/account")); } else { ModelState.AddModelError("form", result.Errors.First()); } return(View("customers/register", WorkContext)); }