Beispiel #1
0
 private void BindUserProfile(UserProfile userProfile)
 {
     if (userProfile != null)
     {
         var decryptedPassword = crypto.Decrypt(userProfile.Password);
         this.txtPassword.Text = userProfile.RememberPassword ? decryptedPassword : string.Empty;
         this.chkRememberPassword.Checked = userProfile.RememberPassword;
         this.chkAutomaticLogin.Checked = userProfile.AutoLogin;
     }
     else
     {
         this.txtPassword.Text = string.Empty;
         this.chkRememberPassword.Checked = false;
         this.chkAutomaticLogin.Checked = false;
     }
 }
Beispiel #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;
            }
        }