Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        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.StorefrontSecurityGetUserByLoginAsync(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.StorefrontSecurityCreateAsync(newUser);

                if (result.Succeeded == true)
                {
                    var storefrontUser = await _commerceCoreApi.StorefrontSecurityGetUserByNameAsync(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));
        }