Exemplo n.º 1
0
        /// <summary>
        /// Displays requested screen section based on received request.
        /// </summary>
        private void ManageScreenVisualization()
        {
            // Initially hide everything on all screens
            LoginSection.Visibility          = Visibility.Collapsed;
            AccessDeniedSection.Visibility   = Visibility.Collapsed;
            ChangePasswordSection.Visibility = Visibility.Collapsed;

            TextBlockApplicationLogin.Visibility = Visibility.Collapsed;
            TextBlockAccessDenied.Visibility     = Visibility.Collapsed;
            TextBlockChangePassword.Visibility   = Visibility.Collapsed;

            // Reset all default buttons
            ButtonLogin.IsDefault  = false;
            ButtonOK.IsDefault     = false;
            ButtonChange.IsDefault = false;

            if (m_displayType == DisplayType.Login)
            {
                TextBoxPassword.Password             = "";
                ButtonLogin.IsDefault                = true;
                TextBlockApplicationLogin.Visibility = Visibility.Visible;
                LoginSection.Visibility              = Visibility.Visible;

                if (string.IsNullOrWhiteSpace(TextBoxUserName.Text))
                {
                    TextBoxUserName.Focus();
                }
                else
                {
                    TextBoxPassword.Focus();
                }
            }
            else if (m_displayType == DisplayType.AccessDenied)
            {
                ButtonOK.IsDefault = true;
                TextBlockAccessDenied.Visibility = Visibility.Visible;
                AccessDeniedSection.Visibility   = Visibility.Visible;
            }
            else if (m_displayType == DisplayType.ChangePassword)
            {
                TextBoxOldPassword.Password        = "";
                TextBoxNewPassword.Password        = "";
                TextBoxConfirmPassword.Password    = "";
                ButtonChange.IsDefault             = true;
                TextBlockChangePassword.Visibility = Visibility.Visible;
                ChangePasswordSection.Visibility   = Visibility.Visible;

                if (string.IsNullOrWhiteSpace(TextBoxChangePasswordUserName.Text))
                {
                    TextBoxChangePasswordUserName.Focus();
                }
                else
                {
                    TextBoxOldPassword.Focus();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to change user's password.
        /// </summary>
        /// <param name="sender">Source of this event.</param>
        /// <param name="e">Arguments of this event.</param>
        private void ButtonChange_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Check if old and new password are different
                if (TextBoxOldPassword.Password == TextBoxNewPassword.Password)
                {
                    throw new Exception("New password cannot be same as old password.");
                }

                // Check is new password and confirm password are same
                if (TextBoxNewPassword.Password != TextBoxConfirmPassword.Password)
                {
                    throw new Exception("New password and confirm password should be same.");
                }

                ISecurityProvider securityProvider = SecurityProviderCache.CreateProvider(TextBoxChangePasswordUserName.Text);
                securityProvider.SecurePassword = TextBoxNewPassword.SecurePassword;

                if (securityProvider.CanChangePassword)
                {
                    // Attempt to change password
                    if (securityProvider.ChangePassword(TextBoxOldPassword.Password, TextBoxNewPassword.Password) &&
                        securityProvider.Authenticate())
                    {
                        // Password changed and authenticated successfully
                        DisplayErrorMessage("Password changed successfully.");

                        // Setup security principal for subsequent uses
                        SecurityIdentity securityIdentity = new SecurityIdentity(securityProvider);
                        SecurityPrincipal = new SecurityPrincipal(securityIdentity);
                        ClearErrorMessage();
                        ExitSuccess = true;
                    }
                    else
                    {
                        // Show why password change failed
                        if (!ShowFailureReason(securityProvider))
                        {
                            if (!securityProvider.IsUserAuthenticated)
                            {
                                DisplayErrorMessage("Authentication was not successful.");
                            }
                            else
                            {
                                DisplayErrorMessage("Password change was not successful.");
                            }

                            if (string.IsNullOrWhiteSpace(TextBoxChangePasswordUserName.Text))
                            {
                                TextBoxChangePasswordUserName.Focus();
                            }
                            else
                            {
                                TextBoxOldPassword.Focus();
                            }
                        }
                    }
                }
                else
                {
                    DisplayErrorMessage("Account does not support password change.");
                }
            }
            catch (Exception ex)
            {
                DisplayErrorMessage("Change password failed: " + ex.Message);
                TextBoxOldPassword.Focus();
            }
        }