public ActionResult RegisterExistingUser(CommerceCustomer commerceUser)
        {
            try
            {
                Assert.ArgumentNotNull(commerceUser, "commerceUser");
                RegisterBaseResultApiModel result = new RegisterBaseResultApiModel();

                var userResponse = this.AccountManager.GetUser(commerceUser.Name);
                if (userResponse.Result == null)
                {
                    // create the user in Sitecore
                    var inputModel = new RegisterUserInputModel {
                        UserName = commerceUser.Name, Password = System.Web.Security.Membership.GeneratePassword(8, 4)
                    };
                    inputModel.FirstName = commerceUser.Properties["FirstName"] as string ?? string.Empty;
                    inputModel.LastName  = commerceUser.Properties["LastName"] as string ?? string.Empty;
                    var response = this.AccountManager.RegisterUser(inputModel);
                    if (!response.ServiceProviderResult.Success || response.Result == null)
                    {
                        result.SetErrors(response.ServiceProviderResult);
                        return(this.Json(result, JsonRequestBehavior.AllowGet));
                    }
                }

                var isLoggedIn = this.AccountManager.Login(commerceUser.Name, false);
                if (isLoggedIn)
                {
                    return(this.Redirect("/"));
                }
                else
                {
                    result.SetErrors(new List <string> {
                        "Could not create user"
                    });
                }

                return(this.Json(result));
            }
            catch (Sitecore.Commerce.OpenIDConnectionClosedUnexpectedlyException)
            {
                this.CleanNotAuthorizedSession();
                return(this.Redirect("/login"));
            }
            catch (Exception e)
            {
                return(this.Json(new ErrorApiModel("Register", e), JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult Register(RegisterUserInputModel inputModel)
        {
            RegisterBaseResultApiModel result = new RegisterBaseResultApiModel();

            try
            {
                Assert.ArgumentNotNull(inputModel, "RegisterInputModel");

                if (string.Equals(inputModel.SignupSelection, "NewAccount", StringComparison.OrdinalIgnoreCase))
                {
                    inputModel.Password = System.Web.Security.Membership.GeneratePassword(8, 4);
                    var response = this.AccountManager.RegisterUser(inputModel);
                    if (response.ServiceProviderResult.Success && response.Result != null)
                    {
                        var isLoggedIn = this.AccountManager.Login(response.Result.UserName, false);
                        if (!isLoggedIn)
                        {
                            result.Success = false;
                            result.SetErrors(new List <string> {
                                "Could not create user"
                            });
                        }
                    }
                    else
                    {
                        result.Success = false;
                        result.SetErrors(response.ServiceProviderResult);
                    }

                    return(this.Json(result, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    string emailOfExistingCustomer = inputModel.LinkupEmail;

                    var response = this.AccountManager.InitiateLinkToExistingCustomer(emailOfExistingCustomer);
                    if (response.ServiceProviderResult.Success && response.Result != null)
                    {
                        ////Clean up auth cookies completely. We need to be signed out.
                        OpenIdConnectUtilities.RemoveCookie(OpenIdConnectUtilities.CookieCurrentProvider);
                        OpenIdConnectUtilities.RemoveCookie(OpenIdConnectUtilities.CookieCurrentProviderType);
                        OpenIdConnectUtilities.RemoveCookie(OpenIdConnectUtilities.OpenIdCookie);

                        result.UserName     = response.Result.Name;
                        result.IsSignupFlow = true;
                        return(this.Json(result, JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        result.Success = false;
                        result.SetErrors(response.ServiceProviderResult);
                        return(this.Json(result, JsonRequestBehavior.AllowGet));
                    }
                }
            }
            catch (AggregateException ex)
            {
                result.Success = false;
                result.SetErrors(StorefrontConstants.KnownActionNames.RegisterActionName, ex.InnerExceptions[0]);
                return(this.Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Sitecore.Commerce.OpenIDConnectionClosedUnexpectedlyException)
            {
                this.CleanNotAuthorizedSession();
                return(this.Redirect("/login"));
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.SetErrors(StorefrontConstants.KnownActionNames.RegisterActionName, ex);
                return(this.Json(result, JsonRequestBehavior.AllowGet));
            }
        }