public async Task <ActionResult> Register(Register formModel) { var user = new coreModel.ApplicationUserExtended { Email = formModel.Email, Password = formModel.Password, UserName = formModel.UserName, UserType = "Customer", StoreId = WorkContext.CurrentStore.Id, }; //Register user in VC Platform (create security account) var result = await _commerceCoreApi.StorefrontSecurity.CreateAsync(user); if (result.Succeeded == true) { //Load newly created account from API var storefrontUser = await _commerceCoreApi.StorefrontSecurity.GetUserByNameAsync(user.UserName); //Next need create corresponding Customer contact in VC Customers (CRM) module //Contacts and account has the same Id. var customer = formModel.ToCustomerInfo(); 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.StorefrontSecurity.PasswordSignInAsync(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 virtual securityDto.ApplicationUserExtended ToUserDto(User user) { var result = new securityDto.ApplicationUserExtended { Email = user.Email, Password = user.Password, UserName = user.UserName, UserType = "Customer", StoreId = user.StoreId, MemberId = user.ContactId }; if (!user.ExternalLogins.IsNullOrEmpty()) { result.Logins = user.ExternalLogins.Select(x => new securityDto.ApplicationUserLogin { LoginProvider = x.LoginProvider, ProviderKey = x.ProviderKey }).ToList(); } return(result); }
public async Task <ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await _authenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest)); } CustomerInfo customer; var user = await _commerceCoreApi.StorefrontSecurity.GetUserByLoginAsync(loginInfo.Login.LoginProvider, loginInfo.Login.ProviderKey); if (user != null) { customer = await GetStorefrontCustomerByUserAsync(user); } else { var newUser = new coreModel.ApplicationUserExtended { Email = loginInfo.Email, UserName = string.Join("--", loginInfo.Login.LoginProvider, loginInfo.Login.ProviderKey), UserType = "Customer", StoreId = WorkContext.CurrentStore.Id, Logins = new List <coreModel.ApplicationUserLogin> { new coreModel.ApplicationUserLogin { LoginProvider = loginInfo.Login.LoginProvider, ProviderKey = loginInfo.Login.ProviderKey } } }; var result = await _commerceCoreApi.StorefrontSecurity.CreateAsync(newUser); if (result.Succeeded == true) { var storefrontUser = await _commerceCoreApi.StorefrontSecurity.GetUserByNameAsync(newUser.UserName); await _customerService.CreateCustomerAsync(new CustomerInfo { Id = storefrontUser.Id, UserId = storefrontUser.Id, UserName = storefrontUser.UserName, FullName = loginInfo.ExternalIdentity.Name, IsRegisteredUser = true, AllowedStores = storefrontUser.AllowedStores }); customer = await GetStorefrontCustomerByUserAsync(storefrontUser); } else { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.InternalServerError)); } } var identity = CreateClaimsIdentity(customer); _authenticationManager.SignIn(identity); await _userLoginEventPublisher.PublishAsync(new UserLoginEvent(WorkContext, WorkContext.CurrentCustomer, customer)); return(StoreFrontRedirect(returnUrl)); }