Ejemplo n.º 1
0
        private void btnSave_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (this.txtCurrentPwd.Text == "")
                {
                    throw new Exception(SharedSupport.GetLocalizedString("ChangePassword_CurrentPassword_RequiredField"));
                }
                else
                {
                    UserM user = UserM.Load(SharedSupport.GetUserIdentity());
                    //Compare the hashed version of the password stored in the db to the hashed version of the password entered.
                    Byte[] passwd    = SharedSupport.ConvertStringToByteArray(this.txtCurrentPwd.Text.Trim());
                    byte[] hashValue = ((HashAlgorithm)CryptoConfig.CreateFromName(Constants.HashMethod)).ComputeHash(passwd);

                    if (user.Password != BitConverter.ToString(hashValue))
                    {
                        throw new Exception(SharedSupport.GetLocalizedString("ChangePassword_CurrentPasswordError"));
                    }
                }
                if (this.txtConfirmPwd.Text == "")
                {
                    throw new Exception(SharedSupport.GetLocalizedString("ChangePassword_ConfirmPassword_RequiredField"));
                }
                if (this.txtNewPwd.Text == "")
                {
                    throw new Exception(SharedSupport.GetLocalizedString("ChangePassword_NewPassword_RequiredField"));
                }

                if (this.txtNewPwd.Text != this.txtConfirmPwd.Text)
                {
                    this.txtConfirmPwd.Text = "";
                    this.txtNewPwd.Text     = "";
                    throw new Exception(SharedSupport.GetLocalizedString("ChangePassword_ConfirmationError"));
                }
                else if ((this.txtNewPwd.Text.Length < 4) || (this.txtNewPwd.Text.Length > 50))
                {
                    this.txtNewPwd.Text     = "";
                    this.txtConfirmPwd.Text = "";
                    throw new Exception(SharedSupport.GetLocalizedString("ChangePassword_PwdLengthError"));
                }
                // New password can't be the same as the previous password
                else if (this.txtNewPwd.Text == this.txtCurrentPwd.Text)
                {
                    this.txtNewPwd.Text     = "";
                    this.txtConfirmPwd.Text = "";
                    throw new Exception(SharedSupport.GetLocalizedString("ChangePassword_PwdSameAsOld"));
                }

                UserM userObj = UserM.Load(SharedSupport.GetUserIdentity());
                if (userObj.IsValid)
                {
                    userObj.Password = txtNewPwd.Text.Trim();
                    userObj.UpdatePassword();
                    Nav1.Feedback.Text = SharedSupport.GetLocalizedString("ChangePassword_SuccessfulUpdateMessage");
                }
            }
            catch (Exception ex)
            {
                Nav1.Feedback.Text = ex.Message;
            }
        }