public ActionResult ChangePassword()
        {
            var context = new SitecoreContext();
            var model   = context.GetCurrentItem <ChangePassword>();

            model.isPasswordPolicyFail  = false;
            model.isShowPasswordUpdated = false;
            SitecoreProfileService scProfileService = new SitecoreProfileService();

            if (Session["isShowPasswordUpdated"] != null)
            {
                model.isShowPasswordUpdated      = (bool)Session["isShowPasswordUpdated"];
                Session["isShowPasswordUpdated"] = null;
            }
            if (Session["ChangePasswordError"] != null)
            {
                List <ModelErrorCollection> allerror = (List <ModelErrorCollection>)Session["ChangePasswordError"];
                foreach (var item in allerror)
                {
                    foreach (var subItem in item)
                    {
                        ModelState.AddModelError("", subItem.ErrorMessage.ToString());
                    }
                }

                Session["ChangePasswordError"] = null;
            }

            if (scProfileService.CheckForDisableAccountUpdates())
            {
                model.isAccountLocked = true;
                ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Profile_AccountNoUpdateAllow"));
            }
            else
            {
                model.isAccountLocked = false;
                if (Request.QueryString["PasswordRuleFail"] != "" && Request.QueryString["PasswordRuleFail"] == "true")
                {
                    var            membershipUser = scProfileService.GetCurrentMembershipUser();
                    ProfileService profileService = new ProfileService();

                    profileService.SetLoginWaitContext(membershipUser.Email, scProfileService.RemoveDomainToUserName(membershipUser.UserName), null, null);

                    model.isPasswordPolicyFail = true;
                }
                else
                {
                    //If the user acecss this page using Self service they MUST be authenticated If not we send them back to the Login page
                    BlueGreenContext bgContext = new BlueGreenContext();
                    if (!bgContext.IsAuthenticated)
                    {
                        Response.Redirect(UrlMapper.Map(model.SiteSettings.SignInPage.Url));
                    }
                }
            }
            return(View(model));
        }
        public ActionResult SignInSitecore(SignIn signIn)
        {
            //Validate if the fields are populated
            if (string.IsNullOrEmpty(signIn.txtEmail) || string.IsNullOrEmpty(signIn.txtPassword))
            {
                var context = new SitecoreContext();
                var model   = context.GetCurrentItem <SignIn>();
                Response.Redirect(UrlMapper.Map(model.SiteSettings.SignInHelpPage.Url));
            }

            //Remove invalid characters from the Email
            if (signIn.txtEmail.Contains(","))
            {
                Session["SignInUiError"] = Sitecore.Globalization.Translate.Text("Profile_AccountNotFound");
                return(base.Index());
            }

            ActionResult result         = null;
            var          profileservice = new ProfileService();

            var loginResponse = profileservice.LoginUser(signIn.txtEmail, signIn.txtPassword, null, null, null, null);

            if (loginResponse.IsSuccessfull)
            {
                var context = new SitecoreContext();
                var model   = context.GetCurrentItem <SignIn>();
                //TODO: Enable Below code once owner object is completely ready
                //RedirectRegistrationConfirmation(UrlMapper.Map(registrationInfo.PostbackSuccessPageUrl));
                Response.Redirect(UrlMapper.Map(model.SiteSettings.SignInWaitPage.Url));
            }
            else
            {
                var context = new SitecoreContext();
                var model   = context.GetCurrentItem <SignIn>();
                if (loginResponse.errorCode == SignInResponse.errors.InvalidPassword)
                {
                    //Session["PasswordRuleFail"] = "true";  //TODO find another way to get this working
                    Response.Redirect("/mybluegreen/my-account/Change-Password?PasswordRuleFail=true", true);  //TODO define const for URL
                }
                SitecoreProfileService scProfileService = new SitecoreProfileService();
                var scUserName = scProfileService.GetUserByEmail(signIn.txtEmail);
                if (loginResponse.errorCode == SignInResponse.errors.LockedAccount && !scProfileService.CheckForDisableAccountUpdates(scUserName))
                {
                    Session["SignInUiError"] = Sitecore.Globalization.Translate.Text("Profile_AccountLocked");
                }
                else
                {
                    Session["SignInUiError"] = Sitecore.Globalization.Translate.Text("Profile_AccountNotFound");
                }
                result = base.Index();
            }

            return(result);
        }
        public ActionResult ForgotPassword(ForgotPassword form)
        {
            if (ModelState.IsValid)
            {
                SitecoreProfileService scProfileService = new SitecoreProfileService();
                ProfileService         profileService   = new ProfileService();

                var scUser = scProfileService.GetUserByEmail(form.txtEmail);

                if (scProfileService.SitecoreExists(scUser))
                {
                    if (scProfileService.CheckForDisableAccountUpdates(scUser))
                    {
                        form.isAccountLocked = true;
                        ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Profile_AccountNoUpdateAllow"));
                    }
                    else
                    {
                        if (EmailManager.ResetEmail(scUser, form.txtEmail))
                        {
                            // ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Profile_forgotPassword_emailsend"));
                            ViewData["message"] = Sitecore.Globalization.Translate.Text("Profile_forgotPassword_emailsend");
                        }
                        else
                        {
                            ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("profile_forgotPassword_emailfail"));
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Profile_ForgotPassword_EmailDonotExist"));
                }
            }

            return(base.Index());
        }
        public ActionResult Register(Registration registrationInfo)
        {
            var profileservice = new ProfileService();

            if (registrationInfo.btnSubmit == "action:lookup")
            {
                foreach (var modelValue in ModelState.Values)
                {
                    modelValue.Errors.Clear();
                }
                if (string.IsNullOrEmpty(registrationInfo.txtLookupSSN) || string.IsNullOrEmpty(registrationInfo.txtLookupPhone))
                {
                    ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Register_AccountNotFound"));
                }
                else
                {
                    var ownerId = profileservice.GetOwnerNumber(registrationInfo.txtLookupSSN, registrationInfo.txtLookupPhone);

                    if (string.IsNullOrEmpty(ownerId))
                    {
                        ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Register_AccountNotFound"));
                    }
                    else
                    {
                        ValueProviderResult temp = new ValueProviderResult(ownerId, ownerId, CultureInfo.InvariantCulture);
                        ModelState["txtOwnerId"].Value = temp;
                    }
                }
            }
            else if (registrationInfo.btnSubmit == "action:register")
            {
                if (ModelState.IsValid)
                {
                    if (registrationInfo.txtPassword.Contains(" "))
                    {
                        ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Profile_PasswordInvalid"));
                    }
                    else
                    {
                        RegisterServiceModel   serviceModel     = new Services.RegisterServiceModel();
                        SitecoreProfileService scProfileService = new SitecoreProfileService();
                        ProfileService         profileService   = new ProfileService();

                        var scUserName = scProfileService.AddDomainToUserName(registrationInfo.txtOwnerId);

                        if (scProfileService.SitecoreExists(scUserName))
                        {
                            var user = scProfileService.GetUser(scUserName);


                            if (user.Profile.Email == registrationInfo.txtAcctEmail)
                            {
                                ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Register_EmailAlreadyRestierToSameOwner"));
                            }
                            else
                            {
                                ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Register_OwnerAlreadyRegister"));
                            }
                        }
                        else
                        {
                            bool allowRegistration = false;
                            var  username          = scProfileService.GetUserByEmail(registrationInfo.txtAcctEmail);
                            if (string.IsNullOrEmpty(username))
                            {
                                var legacyUser = profileService.GetOwnerDemographic(registrationInfo.txtAcctEmail, null);
                                if (legacyUser == null)
                                {
                                    allowRegistration = true;
                                }
                                else if (legacyUser.OwnerId == registrationInfo.txtOwnerId)
                                {
                                    allowRegistration = true;
                                }
                            }
                            //Verify if the email already exists

                            if (allowRegistration)
                            {
                                serviceModel.Email    = registrationInfo.txtAcctEmail;
                                serviceModel.OwnerId  = registrationInfo.txtOwnerId;
                                serviceModel.Password = registrationInfo.txtPassword;
                                serviceModel.Phone    = registrationInfo.txtPhone;
                                serviceModel.SSN      = registrationInfo.txtSSN;

                                if (profileservice.Register(serviceModel))
                                {
                                    var loginResponse = profileservice.LoginUser(registrationInfo.txtAcctEmail, registrationInfo.txtPassword, null, null, null, null);

                                    Session["LoginEmail"]            = registrationInfo.txtAcctEmail;
                                    Session["LoginPassword"]         = registrationInfo.txtPassword;
                                    Session["EnrollAcctNo"]          = registrationInfo.txtOwnerId;
                                    Session["ownerACCT"]             = registrationInfo.txtOwnerId;
                                    Session["ownerRegisterReferrer"] = "Register";
                                    Response.Redirect(UrlMapper.Map(registrationInfo.PostbackSuccessPageUrl), false);
                                    return(null);
                                    //RedirectRegistrationConfirmation(UrlMapper.Map(registrationInfo.PostbackSuccessPageUrl));
                                }
                                else
                                {
                                    ViewData["ShowUnsuccessMessage"] = "true";
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Register_EmailMustBeUnique"));
                            }
                        }
                    }
                }
            }
            return(base.Index());
        }
        public ActionResult ResetPasswordProcess(ResetPassword resetPassword)
        {
            bool isValid = true;

            if (resetPassword.txtNewPassword.Contains(" "))
            {
                if (ModelState.IsValid)
                {
                    ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Profile_PasswordInvalid"));
                }
                isValid = false;
            }

            if (ModelState.IsValid && isValid)
            {
                SitecoreProfileService scProfileService = new SitecoreProfileService();
                var            scUserName = scProfileService.GetUserByEmail(resetPassword.email);
                MembershipUser user       = scProfileService.GetMembershipUser(scUserName);
                if (user != null)
                {
                    user.UnlockUser();

                    scProfileService.ResetFlagPasswordLockedEmail(scUserName);
                    ProfileService profileService = new ProfileService();
                    if (!user.ChangePassword(user.ResetPassword(), resetPassword.txtNewPassword))
                    {
                        ModelState.AddModelError("", "ERROR");  //todo add error
                        return(base.Index());
                    }
                    else
                    {
                        EmailManager.UpdatePassword(scUserName, resetPassword.email);

                        var response = profileService.LoginUser(resetPassword.email, resetPassword.txtNewPassword, null, null, null, null);
                        if (response.IsSuccessfull)
                        {
                            var context = new SitecoreContext();
                            var model   = context.GetCurrentItem <SignIn>();
                            profileService.SetLoginWaitContext(resetPassword.email, null, null, null);
                            Response.Redirect(UrlMapper.Map(model.SiteSettings.SignInWaitPage.Url));
                        }
                        else
                        {
                            ModelState.AddModelError("", "ERROR");  //todo add error
                            return(base.Index());
                        }
                    }
                }
            }
            else
            {
                var errors = ModelState.Select(x => x.Value.Errors)
                             .Where(y => y.Count > 0)
                             .ToList();

                Session["ResetPasswordError"] = errors;

                Response.Redirect(string.Format("/reset-password?email={0}&resetid={1}", resetPassword.email, resetPassword.resetid));
            }
            return(null);
        }
        public ActionResult ResetPassword()
        {
            var context = new SitecoreContext();
            var model   = context.GetCurrentItem <ResetPassword>();

            if (Request.QueryString["email"] == null || Request.QueryString["resetid"] == null)
            {
                WebUtil.Redirect("/Home");
            }

            model.email   = Request.QueryString["email"];
            model.resetid = FormatUtils.ConvertToGuid(Request.QueryString["resetid"]);

            SitecoreProfileService scProfileService = new SitecoreProfileService();

            var            scUserName     = scProfileService.GetUserByEmail(model.email);
            ProfileService profileService = new ProfileService();

            var user = scProfileService.GetUser(scUserName);

            if (Session["ResetPasswordError"] != null)
            {
                List <ModelErrorCollection> allerror = (List <ModelErrorCollection>)Session["ResetPasswordError"];
                foreach (var item in allerror)
                {
                    foreach (var subItem in item)
                    {
                        ModelState.AddModelError("", subItem.ErrorMessage.ToString());
                    }
                }

                Session["ResetPasswordError"] = null;
            }
            if (user != null && user.Profile != null)
            {
                Guid userResetId             = FormatUtils.ConvertToGuid(user.Profile.GetCustomProperty("Forgot Password Unique Id"));
                var  forgotPasswordTimestamp = user.Profile.GetCustomProperty("Forgot Password Timestamp");

                if (Guid.Equals(userResetId, model.resetid))
                {
                    DateTime expireDate = DateUtil.ParseDateTime(forgotPasswordTimestamp, DateTime.MinValue);
                    if (expireDate != DateTime.MinValue)
                    {
                        int forgotPasswordExpirationTime = Int32.Parse(Sitecore.Configuration.Settings.GetSetting("ForgotPasswordExpirationTime"));

                        if (DateTime.Now > expireDate.AddHours(forgotPasswordExpirationTime))
                        {
                            model.hideUIElement = true;
                            ViewData["message"] = model.MessageLinkExpired;

                            // WebUtil.Redirect("/Home");
                        }
                    }
                }
                else
                {
                    model.hideUIElement = true;
                    ViewData["message"] = model.MessageLinkInvalid;
                    //WebUtil.Redirect("/Home");
                }
            }
            return(View(model));
        }
        public ActionResult ChangePasswordProcess(ChangePassword changePassword)
        {
            var            context = new SitecoreContext();
            ChangePassword model   = context.GetCurrentItem <ChangePassword>();

            if (!changePassword.isPasswordPolicyFail && !Context.User.IsAuthenticated)
            {
                Response.Redirect(UrlMapper.Map(model.SiteSettings.SignInPage.Url));
                return(null);
            }

            model.isPasswordPolicyFail = changePassword.isPasswordPolicyFail;  //Reset the default value
            if (ModelState.IsValid)
            {
                if (changePassword.txtNewPassword.Contains(" "))
                {
                    ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("Profile_PasswordInvalid"));
                }
                else
                {
                    SitecoreProfileService scProfileService = new SitecoreProfileService();

                    var membershipUser = scProfileService.GetCurrentMembershipUser();
                    if (membershipUser.ChangePassword(changePassword.txtCurrentPassword, changePassword.txtNewPassword))
                    {
                        EmailManager.UpdatePassword(membershipUser.UserName, membershipUser.Email);
                        if (changePassword.isPasswordPolicyFail)// Need to Complete the login Proces
                        {
                            Response.Redirect(UrlMapper.Map(model.SiteSettings.SignInWaitPage.Url));
                            return(null);
                        }
                        else
                        {
                            Session["isShowPasswordUpdated"] = true;
                            //  model.isShowPasswordUpdated = true;
                            return(Redirect(UrlMapper.Map(SitecoreUtils.GetPageUrl(SitecoreItemReferences.ChangePasswordPageId))));
                        }
                    }
                    else
                    {
                        MembershipUser user = Membership.GetUser(Context.User.Name, false);
                        if (user != null)
                        {
                            if (user.IsLockedOut)
                            {
                                var scUser = scProfileService.GetUser(Context.User.Name);
                                Components.EmailManager.ResetEmail(Context.User.Name, scUser.Profile.Email);
                                if (scUser != null && !scProfileService.CheckForPasswordLockedEmail(scUser))
                                {
                                    scUser.Profile.SetCustomProperty(SitecoreProfileService.PasswordLockedEmailId, "1");
                                    scUser.Profile.Save();
                                }
                                Session["SignInUiError"] = Sitecore.Globalization.Translate.Text("Profile_AccountLocked");
                                return(Redirect(UrlMapper.Map(model.SiteSettings.SignInPage.Url)));
                            }
                        }
                        ModelState.AddModelError("", Sitecore.Globalization.Translate.Text("CurrentPassword_Current_NotCorrect"));
                    }
                }
            }


            var errors = ModelState.Select(x => x.Value.Errors)
                         .Where(y => y.Count > 0)
                         .ToList();

            if (errors != null && errors.Count > 0)
            {
                Session["ChangePasswordError"] = errors;
            }
            if (changePassword.isPasswordPolicyFail)
            {
                return(Redirect(UrlMapper.Map(SitecoreUtils.GetPageUrl(SitecoreItemReferences.ChangePasswordPageId)) + "?PasswordRuleFail=true"));
            }
            else
            {
                return(Redirect(UrlMapper.Map(SitecoreUtils.GetPageUrl(SitecoreItemReferences.ChangePasswordPageId))));
            }
            //return View(model);
        }