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)); }
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)); }
private async Task <CustomerInfo> GetCustomerAsync(IOwinContext context) { var retVal = new CustomerInfo(); var principal = context.Authentication.User; var identity = principal.Identity; if (identity.IsAuthenticated) { var userId = identity.GetUserId(); if (userId == null) { //If somehow claim not found in user cookies need load user by name from API var user = await _commerceApi.StorefrontSecurityGetUserByNameAsync(identity.Name); if (user != null) { userId = user.Id; } } if (userId != null) { var customerService = _container.Resolve <ICustomerService>(); var customer = await customerService.GetCustomerByIdAsync(userId); retVal = customer ?? retVal; retVal.Id = userId; retVal.UserName = identity.Name; retVal.IsRegisteredUser = true; } retVal.OperatorUserId = principal.FindFirstValue(StorefrontConstants.OperatorUserIdClaimType); retVal.OperatorUserName = principal.FindFirstValue(StorefrontConstants.OperatorUserNameClaimType); var allowedStores = principal.FindFirstValue(StorefrontConstants.AllowedStoresClaimType); if (!string.IsNullOrEmpty(allowedStores)) { retVal.AllowedStores = allowedStores.Split(','); } } if (!retVal.IsRegisteredUser) { retVal.Id = context.Request.Cookies[StorefrontConstants.AnonymousCustomerIdCookie]; retVal.UserName = StorefrontConstants.AnonymousUsername; retVal.FullName = StorefrontConstants.AnonymousUsername; } return(retVal); }