Exemplo n.º 1
0
        /// <summary>
        /// Authenticates the specified document.
        /// </summary>
        /// <param name="decoder">The decoder.</param>
        public static bool Authenticate(DecoderBase decoder)
        {
            // if authentication is required
            if (decoder.IsAuthenticationRequired)
            {
                // get the document filename
                string filename = null;
                if (decoder.SourceStream != null && decoder.SourceStream is FileStream)
                {
                    filename = ((FileStream)decoder.SourceStream).Name;
                }

                while (true)
                {
                    // create the document password dialog
                    using (DocumentPasswordForm enterPasswordDialog = new DocumentPasswordForm())
                    {
                        enterPasswordDialog.Filename = filename;

                        // show the document password dialog
                        DialogResult showDialogResult = enterPasswordDialog.ShowDialog();

                        // if "OK" button is clicked
                        if (showDialogResult == DialogResult.OK)
                        {
                            DocumentAuthenticationRequest authenticationRequest = new DocumentAuthenticationRequest(
                                enterPasswordDialog.authenticateTypeComboBox.Text,
                                enterPasswordDialog.Password);

                            // uuthenticate the decoder
                            DocumentAuthorizationResult result = decoder.Authenticate(authenticationRequest);
                            // if user is NOT authorized
                            if (!result.IsAuthorized)
                            {
                                MessageBox.Show(
                                    string.Format("The {0} password is incorrect.", enterPasswordDialog.authenticateTypeComboBox.Text),
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            // if user is authorized
                            else
                            {
                                if (enterPasswordDialog.authenticateTypeComboBox.Text != result.UserName)
                                {
                                    MessageBox.Show(
                                        string.Format("Authorized as: {0}", result.UserName),
                                        "Authorization Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                break;
                            }
                        }
                        // if "Cancel" button is clicked
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }