public async Task <ActionResult> Register(Register formModel)
        {
            var user = new coreModel.ApplicationUserExtended
            {
                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));
        }
Example #2
0
        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);
        }