コード例 #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;
                    }
                }
            }
        }
コード例 #2
0
ファイル: FrmRegister.cs プロジェクト: andyshao/CloudNotes
 private void btnOK_Click(object sender, EventArgs e)
 {
     errorProvider.Clear();
     this.DialogResult = DialogResult.OK;
     if (string.IsNullOrEmpty(txtUserName.Text))
     {
         errorProvider.SetError(txtUserName, Resources.UserNameRequired);
         txtUserName.Focus();
         this.DialogResult = DialogResult.None;
         return;
     }
     if (string.IsNullOrEmpty(txtPassword.Text))
     {
         errorProvider.SetError(txtPassword, Resources.PasswordRequired);
         txtPassword.Focus();
         this.DialogResult = DialogResult.None;
         return;
     }
     if (string.IsNullOrEmpty(txtConfirm.Text))
     {
         errorProvider.SetError(txtConfirm, Resources.ConfirmPasswordRequired);
         txtConfirm.Focus();
         this.DialogResult = DialogResult.None;
         return;
     }
     if (string.Compare(txtPassword.Text, txtConfirm.Text, StringComparison.InvariantCultureIgnoreCase) != 0)
     {
         errorProvider.SetError(txtConfirm, Resources.IncorrectConfirmPassword);
         txtConfirm.Focus();
         this.DialogResult = DialogResult.None;
         return;
     }
     if (string.IsNullOrEmpty(txtEmail.Text))
     {
         errorProvider.SetError(txtEmail, Resources.EmailRequired);
         txtEmail.Focus();
         this.DialogResult = DialogResult.None;
         return;
     }
     var regex = new Regex(Constants.EmailAddressFormatPattern);
     if (!regex.IsMatch(txtEmail.Text))
     {
         errorProvider.SetError(txtEmail, Resources.InvalidEmailAddress);
         txtEmail.Focus();
         this.DialogResult = DialogResult.None;
         return;
     }
     if (string.IsNullOrEmpty(txtServer.Text))
     {
         errorProvider.SetError(txtServer, Resources.ServerRequired);
         txtServer.Focus();
         this.DialogResult = DialogResult.None;
         return;
     }
     try
     {
         txtUserName.Enabled = false;
         txtPassword.Enabled = false;
         txtConfirm.Enabled = false;
         txtEmail.Enabled = false;
         txtServer.Enabled = false;
         btnOK.Enabled = false;
         using (
             var proxy =
                 new ServiceProxy(
                     new ClientCredential
                     {
                         UserName = Crypto.ProxyUserName,
                         Password = Crypto.ProxyUserPassword,
                         ServerUri = txtServer.Text.Trim()
                     }))
         {
             var result =
                 proxy.PutAsJsonAsync(
                     "api/users/create",
                     new
                     {
                         UserName = txtUserName.Text.Trim(),
                         Password =
                             Convert.ToBase64String(
                                 Encoding.ASCII.GetBytes(
                                     Crypto.ComputeHash(txtPassword.Text.Trim(), txtUserName.Text.Trim()))),
                         Email = txtEmail.Text.Trim()
                     }).Result;
             // Here we cannot use the async/await feature, since
             // the dialog will be closed even if we set the DialogResult to None.
             if (result.IsSuccessStatusCode)
             {
                 this.UserName = txtUserName.Text.Trim();
                 this.Password = txtPassword.Text.Trim();
                 this.ServerUri = txtServer.Text.Trim();
                 MessageBox.Show(
                     Resources.CreateAccountSuccessful,
                     this.Text,
                     MessageBoxButtons.OK,
                     MessageBoxIcon.Information);
             }
             else
             {
                 dynamic details = JsonConvert.DeserializeObject(result.Content.ReadAsStringAsync().Result);
                 if (details != null)
                 {
                     MessageBox.Show(
                         details.ExceptionMessage.ToString(),
                         Resources.Error,
                         MessageBoxButtons.OK,
                         MessageBoxIcon.Error);
                 }
                 else
                 {
                     MessageBox.Show("Register failed!", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
                 this.DialogResult = DialogResult.None;
             }
         }
     }
     catch (Exception ex)
     {
         FrmExceptionDialog.ShowException(ex);
         this.DialogResult = DialogResult.None;
     }
     finally
     {
         txtUserName.Enabled = true;
         txtPassword.Enabled = true;
         txtConfirm.Enabled = true;
         txtEmail.Enabled = true;
         txtServer.Enabled = true;
         btnOK.Enabled = true;
     }
 }
コード例 #3
0
ファイル: FrmLogin.cs プロジェクト: hhqqnu/CloudNotes
        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;
            }
        }