Esempio n. 1
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            var hasError = false;
            if (string.IsNullOrEmpty(txtOldPassword.Text))
            {
                errorProvider.SetError(txtOldPassword, Resources.OriginalPasswordRequired);
                hasError = true;
            }
            if (string.IsNullOrEmpty(txtNewPassword.Text))
            {
                errorProvider.SetError(txtNewPassword, Resources.NewPasswordRequired);
                hasError = true;
            }
            if (string.IsNullOrEmpty(txtConfirmPassword.Text))
            {
                errorProvider.SetError(txtConfirmPassword, Resources.ConfirmPasswordRequired);
                hasError = true;
            }
            if (string.Compare(txtNewPassword.Text, txtConfirmPassword.Text, StringComparison.InvariantCulture) != 0)
            {
                errorProvider.SetError(txtConfirmPassword, Resources.IncorrectConfirmPassword);
                hasError = true;
            }
            if (hasError)
            {
                this.DialogResult = DialogResult.None;
            }
            else
            {
                oldPassword = txtOldPassword.Text;
                newPassword = txtNewPassword.Text;

                var encodedOldPassword =
                    Convert.ToBase64String(
                        Encoding.ASCII.GetBytes(Crypto.ComputeHash(oldPassword, this.clientCredential.UserName)));
                var encodedNewPassword =
                    Convert.ToBase64String(
                        Encoding.ASCII.GetBytes(Crypto.ComputeHash(newPassword, this.clientCredential.UserName)));

                using (var proxy = new ServiceProxy(this.clientCredential))
                {
                    try
                    {
                        txtConfirmPassword.Enabled = false;
                        txtNewPassword.Enabled = false;
                        txtOldPassword.Enabled = false;
                        btnOK.Enabled = false;
                        var result =
                            proxy.PostAsJsonAsync(
                                "api/users/password/change",
                                new
                                {
                                    clientCredential.UserName,
                                    EncodedOldPassword = encodedOldPassword,
                                    EncodedNewPassword = encodedNewPassword
                                }).Result;
                        switch (result.StatusCode)
                        {
                            case HttpStatusCode.OK:
                                // Re-assign the new password to client credential
                                clientCredential.Password = newPassword;
                                MessageBox.Show(
                                    Resources.PasswordChangedSuccessfully,
                                    Resources.Information,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                                break;
                            case HttpStatusCode.Forbidden:
                                MessageBox.Show(
                                    Resources.IncorrectUserNamePassword,
                                    Resources.Error,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                                this.DialogResult = DialogResult.None;
                                return;
                            default:
                                var message = result.Content.ReadAsStringAsync().Result;
                                MessageBox.Show(
                                    message,
                                    Resources.Error,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                                this.DialogResult = DialogResult.None;
                                return;
                        }
                    }
                    catch (Exception ex)
                    {
                        FrmExceptionDialog.ShowException(ex);
                        this.DialogResult = DialogResult.None;
                    }
                    finally
                    {
                        txtConfirmPassword.Enabled = true;
                        txtNewPassword.Enabled = true;
                        txtOldPassword.Enabled = true;
                        btnOK.Enabled = true;
                    }
                }
            }
        }
Esempio n. 2
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            var hasError = false;
            this.errorProvider.Clear();
            if (string.IsNullOrWhiteSpace(cbUserName.Text))
            {
                hasError = true;
                errorProvider.SetError(cbUserName, Resources.UserNameRequired);
            }
            if (string.IsNullOrWhiteSpace(txtPassword.Text))
            {
                hasError = true;
                errorProvider.SetError(txtPassword, Resources.PasswordRequired);
            }
            if (string.IsNullOrWhiteSpace(cbServer.Text))
            {
                hasError = true;
                errorProvider.SetError(cbServer, Resources.ServerRequired);
            }
            if (hasError)
            {
                // prevent the dialog from closing
                this.DialogResult = DialogResult.None;
                return;
            }

            Credential = new ClientCredential
            {
                Password = txtPassword.Text,
                ServerUri = cbServer.Text.Trim(),
                UserName = cbUserName.Text.Trim()
            };

            // reset the IsSelected property for users
            profile.UserProfiles.ForEach(up => up.IsSelected = false);
            var userProfile = profile.UserProfiles.FirstOrDefault(p => p.UserName == Credential.UserName);
            var encryptedPassword = crypto.Encrypt(Credential.Password);
            if (userProfile == null)
            {
                userProfile = new UserProfile
                {
                    AutoLogin = chkAutomaticLogin.Checked,
                    IsSelected = true,
                    Password = encryptedPassword,
                    RememberPassword = chkRememberPassword.Checked,
                    UserName = Credential.UserName
                };
                profile.UserProfiles.Add(userProfile);
            }
            else
            {
                userProfile.AutoLogin = chkAutomaticLogin.Checked;
                userProfile.IsSelected = true;
                userProfile.Password = encryptedPassword;
                userProfile.RememberPassword = chkRememberPassword.Checked;
                userProfile.UserName = Credential.UserName;
            }
            // reset the IsSelected property for servers
            profile.ServerProfiles.ForEach(sp => sp.IsSelected = false);
            var serverProfile = profile.ServerProfiles.FirstOrDefault(p => p.Uri == Credential.ServerUri);
            if (serverProfile == null)
            {
                serverProfile = new ServerProfile(Credential.ServerUri) {IsSelected = true};
                profile.ServerProfiles.Add(serverProfile);
            }
            else
            {
                serverProfile.IsSelected = true;
                serverProfile.Uri = cbServer.Text;
            }

            try
            {
                cbUserName.Enabled = false;
                txtPassword.Enabled = false;
                cbServer.Enabled = false;
                btnOK.Enabled = false;
                this.Cursor = Cursors.WaitCursor;
                using (var proxy = new ServiceProxy(Credential))
                {
                    var result =
                        proxy.PostAsJsonAsync(
                            "api/users/authenticate",
                            new
                            {
                                Credential.UserName,
                                EncodedPassword =
                                    Convert.ToBase64String(
                                        Encoding.ASCII.GetBytes(Crypto.ComputeHash(Credential.Password,
                                            Credential.UserName)))
                            }).Result;
                    switch (result.StatusCode)
                    {
                        case HttpStatusCode.OK:
                            break;
                        case HttpStatusCode.Forbidden:
                            MessageBox.Show(
                                Resources.IncorrectUserNamePassword,
                                Resources.Error,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                            this.DialogResult = DialogResult.None;
                            return;
                        default:
                            dynamic message = JsonConvert.DeserializeObject(result.Content.ReadAsStringAsync().Result);
                            MessageBox.Show(
                                message.ExceptionMessage.ToString(),
                                Resources.Error,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                            this.DialogResult = DialogResult.None;
                            return;
                    }
                }

                Profile.Save(fileName, profile);
            }
            catch (Exception ex)
            {
                FrmExceptionDialog.ShowException(ex);
                this.DialogResult = DialogResult.None;
            }
            finally
            {
                cbUserName.Enabled = true;
                txtPassword.Enabled = true;
                cbServer.Enabled = true;
                btnOK.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }