Ejemplo n.º 1
0
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                User   user            = AbleContext.Current.User;
                string currentUserName = user.UserName;

                bool validPassword;
                if (!user.IsAnonymousOrGuest)
                {
                    validPassword = Membership.ValidateUser(currentUserName, CurrentPassword.Text);
                    if (!validPassword)
                    {
                        InvalidPassword.IsValid = false;
                        return;
                    }
                }
                else
                {
                    validPassword = true;
                }

                // IF USERNAME IS CHANGED, VALIDATE THE NEW NAME IS AVAILABLE
                string newUserName     = UserName.Text.Trim();
                bool   userNameChanged = (currentUserName != newUserName);
                if (userNameChanged)
                {
                    // CHECK IF THERE IS ALREADY A USER WITH DESIRED USERNAME
                    if (UserDataSource.GetUserIdByUserName(newUserName) > 0)
                    {
                        // A USER ALREADY EXISTS WITH THAT NAME
                        phUserNameUnavailable.Visible = true;
                        return;
                    }
                }

                // UPDATE THE USER RECORD WITH NEW VALUES
                user.Email = Email.Text.Trim();
                user.PrimaryAddress.Email = user.Email;
                user.UserName             = newUserName;
                user.Save();

                // RESET AUTH COOKIE WITH NEW USERNAME IF NEEDED
                if (userNameChanged)
                {
                    FormsAuthentication.SetAuthCookie(newUserName, false);
                }

                // DISPLAY RESULT
                ConfirmationMsg.Visible = true;
            }
        }
 private bool ValidateNewUserName(string newUserName)
 {
     if (!_User.UserName.Equals(newUserName, StringComparison.InvariantCultureIgnoreCase))
     {
         //user name has been changed. verify if new user name is available
         int existingUserId = UserDataSource.GetUserIdByUserName(newUserName);
         if (existingUserId > 0)
         {
             UserNameAvailableValidator.ErrorMessage = string.Format(UserNameAvailableValidator.ErrorMessage, newUserName);
             UserNameAvailableValidator.IsValid      = false;
             return(false);
         }
     }
     return(true);
 }
        protected void RegisterButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid && ValidatePassword())
            {
                if ((!trCaptchaField.Visible) || CaptchaImage.Authenticate(CaptchaInput.Text))
                {
                    // PERFORM CUSTOM VALIDATION TO ENSURE EMAIL IS NOT ALREADY REGISTERED
                    string userName = UserName.Text.Trim();
                    int    userIde  = UserDataSource.GetUserIdByEmail(userName);
                    int    userIdu  = UserDataSource.GetUserIdByUserName(userName);
                    if (userIde == 0 && userIdu == 0)
                    {
                        // NO USER REGISTERED WITH THAT USERNAME OR EMAIL
                        MembershipCreateStatus status;
                        User newUser = UserDataSource.CreateUser(userName, userName, Password.Text, string.Empty, string.Empty, true, 0, out status);
                        if (status == MembershipCreateStatus.Success)
                        {
                            // WE HAVE TO VALIDATE CREDENTIALS SO A MODIFIED FORM POST CANNOT ACCESS THIS CODE
                            if (Membership.ValidateUser(userName, Password.Text))
                            {
                                // SET A DEFAULT BILLING ADDRESS FOR THE USER
                                newUser.PrimaryAddress.Email       = userName;
                                newUser.PrimaryAddress.CountryCode = AbleContext.Current.Store.DefaultWarehouse.CountryCode;
                                newUser.PrimaryAddress.Residence   = true;
                                newUser.Save();

                                // SET COOKIE TO REMEMBER USERNAME IF INDICATED
                                if (RememberUserName.Checked)
                                {
                                    HttpCookie cookie = new HttpCookie("UserName", userName);
                                    cookie.Expires = DateTime.MaxValue;
                                    Response.Cookies.Add(cookie);
                                }
                                else
                                {
                                    Response.Cookies.Add(new HttpCookie("UserName", ""));
                                }

                                //MIGRATE USER IF NEEDED
                                int newUserId = UserDataSource.GetUserIdByUserName(userName);
                                if ((AbleContext.Current.UserId != newUserId) && (newUserId != 0))
                                {
                                    User.Migrate(AbleContext.Current.User, newUser, false, true);
                                    AbleContext.Current.UserId = newUserId;
                                }

                                //REDIRECT TO APPROPRIATE PAGE
                                FormsAuthentication.SetAuthCookie(UserName.Text, false);
                                Response.Redirect(NavigationHelper.GetReturnUrl(NavigationHelper.GetMobileStoreUrl("~/Default.aspx")));
                            }
                        }
                        else
                        {
                            InvalidRegistration.IsValid = false;
                            switch (status)
                            {
                            case MembershipCreateStatus.DuplicateUserName:
                            case MembershipCreateStatus.DuplicateEmail:
                                InvalidRegistration.ErrorMessage = "The user-name you have provided is already registered.  Sign in to access your account.";
                                break;

                            case MembershipCreateStatus.InvalidEmail:
                                InvalidRegistration.ErrorMessage = "The email address you have provided is not valid.";
                                break;

                            case MembershipCreateStatus.InvalidUserName:
                                InvalidRegistration.ErrorMessage = "The user-name you have provided is not valid.";
                                break;

                            case MembershipCreateStatus.InvalidPassword:
                                InvalidRegistration.ErrorMessage = "The password you have provided is not valid.";
                                break;

                            default:
                                InvalidRegistration.ErrorMessage = "Unexpected error in registration (" + status.ToString() + ")";
                                break;
                            }
                        }
                    }
                    else
                    {
                        DuplicateEmailValidator.IsValid = false;
                    }
                }
                else
                {
                    //CAPTCHA IS VISIBLE AND DID NOT AUTHENTICATE
                    CustomValidator invalidInput = new CustomValidator();
                    invalidInput.ID           = Guid.NewGuid().ToString();
                    invalidInput.Text         = "*";
                    invalidInput.ErrorMessage = "You did not input the verification number correctly.";
                    invalidInput.IsValid      = false;
                    phCaptchaValidators.Controls.Add(invalidInput);
                    CaptchaInput.Text = "";
                    Password.Attributes.Add("value", string.Empty);
                    RefreshCaptcha();
                }
            }
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                User   user            = AbleContext.Current.User;
                string currentUserName = user.UserName;

                // VALIDATE THE PASSWORD IF THIS IS NOT AN ANONYMOUS USER
                bool validPassword;
                if (!user.IsAnonymousOrGuest)
                {
                    validPassword = Membership.ValidateUser(currentUserName, CurrentPassword.Text);
                    if (!validPassword)
                    {
                        InvalidPassword.IsValid = false;
                        return;
                    }
                }
                else
                {
                    validPassword = true;
                }

                // VALIDATE NEW PASSWORD AGASINT POLICY
                if (Password.Text.Length > 0)
                {
                    PasswordPolicy policy;
                    if (user.IsAdmin)
                    {
                        policy = new MerchantPasswordPolicy();
                    }
                    else
                    {
                        policy = new CustomerPasswordPolicy();
                    }
                    PasswordTestResult result = policy.TestPasswordWithFeedback(user, Password.Text);
                    if ((result & PasswordTestResult.Success) != PasswordTestResult.Success)
                    {
                        PasswordPolicyValidator.ErrorMessage += "<UL>";
                        if ((result & PasswordTestResult.PasswordTooShort) == PasswordTestResult.PasswordTooShort)
                        {
                            AddPwdValidationError(string.Format(PasswordPolicyLength.Text, policy.MinLength));
                        }
                        if ((result & PasswordTestResult.RequireLower) == PasswordTestResult.RequireLower)
                        {
                            AddPwdValidationError("New password must contain at least one lowercase letter.");
                        }
                        if ((result & PasswordTestResult.RequireUpper) == PasswordTestResult.RequireUpper)
                        {
                            AddPwdValidationError("New password must contain at least one uppercase letter. ");
                        }
                        if ((result & PasswordTestResult.RequireNonAlpha) == PasswordTestResult.RequireNonAlpha)
                        {
                            AddPwdValidationError("New password must contain at least one non-letter.");
                        }
                        if ((result & PasswordTestResult.RequireNumber) == PasswordTestResult.RequireNumber)
                        {
                            AddPwdValidationError("New password must contain at least one number.");
                        }
                        if ((result & PasswordTestResult.RequireSymbol) == PasswordTestResult.RequireSymbol)
                        {
                            AddPwdValidationError("New password must contain at least one symbol.");
                        }
                        if ((result & PasswordTestResult.PasswordHistoryLimitation) == PasswordTestResult.PasswordHistoryLimitation)
                        {
                            AddPwdValidationError("You have recently used this password.");
                        }
                        PasswordPolicyValidator.ErrorMessage += "</UL>";
                        PasswordPolicyValidator.IsValid       = false;
                        return;
                    }
                }
                else if (user.IsAnonymousOrGuest)
                {
                    // PASSWORD IS REQUIRED FOR NEW ANONYMOUS ACCOUNTS
                    PasswordRequiredValidator.IsValid = false;
                    return;
                }

                // IF USERNAME IS CHANGED, VALIDATE THE NEW NAME IS AVAILABLE
                string newUserName     = UserName.Text.Trim();
                bool   userNameChanged = (currentUserName != newUserName);
                if (userNameChanged)
                {
                    // CHECK IF THERE IS ALREADY A USER WITH DESIRED USERNAME
                    if (UserDataSource.GetUserIdByUserName(newUserName) > 0)
                    {
                        // A USER ALREADY EXISTS WITH THAT NAME
                        phUserNameUnavailable.Visible = true;
                        return;
                    }
                }

                // OPT-OUT REVIEW REMINDERS
                user.Settings.OptOutReviewReminders = !ReviewReminders.Checked;

                // UPDATE THE USER RECORD WITH NEW VALUES
                user.Email = Email.Text.Trim();
                user.PrimaryAddress.Email = user.Email;
                user.UserName             = newUserName;
                user.Save();

                // RESET AUTH COOKIE WITH NEW USERNAME IF NEEDED
                if (userNameChanged)
                {
                    FormsAuthentication.SetAuthCookie(newUserName, false);
                }

                // UPDATE PASSWORD IF INDICATED
                if (Password.Text.Length > 0)
                {
                    user.SetPassword(Password.Text);
                }

                // UPDATE MAILING PREFERENCES
                if (phEmailLists.Visible)
                {
                    UpdateEmailLists();
                }

                // DISPLAY RESULT
                SavedMessage.Visible = true;
            }
        }
Ejemplo n.º 5
0
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //VERIFY THE NEW PASSWORD MEETS POLICY
                PasswordPolicy policy;
                if (_User.IsAdmin)
                {
                    policy = new MerchantPasswordPolicy();
                }
                else
                {
                    policy = new CustomerPasswordPolicy();
                }

                PasswordTestResult result = policy.TestPasswordWithFeedback(_User, Password.Text);
                if ((result & PasswordTestResult.Success) == PasswordTestResult.Success)
                {
                    // CHECK IF THERE IS ALREADY A USER WITH DESIRED USERNAME
                    if (_User.IsAnonymousOrGuest && UserDataSource.GetUserIdByUserName(_User.Email) == 0)
                    {
                        _User.UserName = _User.Email;
                    }

                    _User.SetPassword(Password.Text);
                    _User.Comment = string.Empty;
                    _User.Save();
                    CommerceBuilder.Users.User.Migrate(AbleContext.Current.User, _User);
                    FormsAuthentication.SetAuthCookie(_User.UserName, false);
                    Response.Redirect(AbleCommerce.Code.NavigationHelper.GetHomeUrl());
                }
                else
                {
                    //Your password did not meet the following minimum requirements
                    if ((result & PasswordTestResult.PasswordTooShort) == PasswordTestResult.PasswordTooShort)
                    {
                        AddPasswordValidator("Password length must be at least " + policy.MinLength.ToString() + " characters.");
                    }
                    if ((result & PasswordTestResult.RequireLower) == PasswordTestResult.RequireLower)
                    {
                        AddPasswordValidator("Password must contain at least one lowercase letter.<br/>");
                    }
                    if ((result & PasswordTestResult.RequireUpper) == PasswordTestResult.RequireUpper)
                    {
                        AddPasswordValidator("Password must contain at least one uppercase letter.<br/> ");
                    }
                    if ((result & PasswordTestResult.RequireNonAlpha) == PasswordTestResult.RequireNonAlpha)
                    {
                        AddPasswordValidator("Password must contain at least one non-letter.<br/> ");
                    }
                    if ((result & PasswordTestResult.RequireNumber) == PasswordTestResult.RequireNumber)
                    {
                        AddPasswordValidator("Password must contain at least one number.<br/> ");
                    }
                    if ((result & PasswordTestResult.RequireSymbol) == PasswordTestResult.RequireSymbol)
                    {
                        AddPasswordValidator("Password must contain at least one symbol.<br/> ");
                    }

                    if ((result & PasswordTestResult.PasswordHistoryLimitation) == PasswordTestResult.PasswordHistoryLimitation)
                    {
                        AddPasswordValidator("You have recently used this password.<br/>");
                    }
                }
            }
        }
Ejemplo n.º 6
0
        protected void Page_Init(object sender, EventArgs e)
        {
            _UserId = AlwaysConvert.ToInt(Request.QueryString["Key"]);
            _User   = UserDataSource.Load(_UserId);
            if ((_User == null) || (!_User.IsApproved))
            {
                Response.Redirect(AbleCommerce.Code.NavigationHelper.GetHomeUrl());
            }
            string tempPassword = AlwaysConvert.ToString(Request.QueryString["Check"]);

            if (string.IsNullOrEmpty(tempPassword) || (_User.Comment != tempPassword))
            {
                Response.Redirect(AbleCommerce.Code.NavigationHelper.GetHomeUrl());
            }
            if (!Page.IsPostBack)
            {
                // CHECK IF THERE IS ALREADY A USER WITH DESIRED USERNAME
                if (_User.IsAnonymousOrGuest && UserDataSource.GetUserIdByUserName(_User.Email) == 0)
                {
                    UserName.Text = _User.Email;
                }
                else
                {
                    UserName.Text = _User.UserName;
                }
                // PASSWORD POLICY
                PasswordPolicy policy;
                if (_User.IsAdmin)
                {
                    policy = new MerchantPasswordPolicy();
                }
                else
                {
                    policy = new CustomerPasswordPolicy();
                }
                PasswordPolicyLength.Text          = string.Format(PasswordPolicyLength.Text, policy.MinLength);
                PasswordPolicyHistoryCount.Visible = (policy.HistoryCount > 0);
                if (PasswordPolicyHistoryCount.Visible)
                {
                    PasswordPolicyHistoryCount.Text = string.Format(PasswordPolicyHistoryCount.Text, policy.HistoryCount);
                }
                PasswordPolicyHistoryDays.Visible = (policy.HistoryDays > 0);
                if (PasswordPolicyHistoryDays.Visible)
                {
                    PasswordPolicyHistoryDays.Text = string.Format(PasswordPolicyHistoryDays.Text, policy.HistoryDays);
                }
                List <string> requirements = new List <string>();
                if (policy.RequireUpper)
                {
                    requirements.Add("uppercase letter");
                }
                if (policy.RequireLower)
                {
                    requirements.Add("lowercase letter");
                }
                if (policy.RequireNumber)
                {
                    requirements.Add("number");
                }
                if (policy.RequireSymbol)
                {
                    requirements.Add("symbol");
                }
                if (!policy.RequireNumber && !policy.RequireSymbol && policy.RequireNonAlpha)
                {
                    requirements.Add("non-letter");
                }
                PasswordPolicyRequired.Visible = (requirements.Count > 0);
                if (PasswordPolicyRequired.Visible)
                {
                    if (requirements.Count > 1)
                    {
                        requirements[requirements.Count - 1] = "and " + requirements[requirements.Count - 1];
                    }
                    PasswordPolicyRequired.Text = string.Format(PasswordPolicyRequired.Text, string.Join(", ", requirements.ToArray()));
                }
            }
        }