Exemple #1
0
        private async Task <string> CreteRequest()
        {
            CertificateRequestProperties certRequestProperties = new CertificateRequestProperties();

            certRequestProperties.Exportable         = ExportOption.NotExportable;
            certRequestProperties.FriendlyName       = "Bigsby Client";
            certRequestProperties.KeyProtectionLevel = KeyProtectionLevel.NoConsent;
            certRequestProperties.KeyUsages          = EnrollKeyUsages.Signing;
            certRequestProperties.Subject            = "BigsbyClient";

            return(await CertificateEnrollmentManager.CreateRequestAsync(certRequestProperties));
        }
Exemple #2
0
        /// <summary>
        /// Create a certificate request
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void CreateRequest_Click(object sender, RoutedEventArgs e)
        {
            TextBlock outputTextBlock = rootPage.FindName("OutputTextBlock") as TextBlock;

            outputTextBlock.Text = "Creating certificate request...";

            try
            {
                //call the default constructor of CertificateRequestProperties
                CertificateRequestProperties reqProp = new CertificateRequestProperties();
                reqProp.Subject      = "Toby";
                reqProp.FriendlyName = "Toby's Cert";

                //call Certificate Enrollment function createRequest to create a certificate request
                certificateRequest = await CertificateEnrollmentManager.CreateRequestAsync(reqProp);

                outputTextBlock.Text += "\nRequest created, content:\n" + certificateRequest;
            }
            catch (Exception ex)
            {
                outputTextBlock.Text += "\n\nCertificate request creation failed with error: " + ex.Message;
            }
        }
Exemple #3
0
        /// <summary>
        /// Submit request to a server then install the returned certificate
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void RunSample_Click(object sender, RoutedEventArgs e)
        {
            TextBlock outputTextBlock   = rootPage.FindName("OutputTextBlock") as TextBlock;
            TextBox   urlTextBox        = rootPage.FindName("UrlTextBox") as TextBox;
            CheckBox  userStoreCheckBox = rootPage.FindName("UserStoreCheckBox") as CheckBox;
            string    response          = "";
            string    url = "";

            outputTextBlock.Text = "Creating certificate request...";

            // validate the URL as entered in "UrlTextBox"
            if (String.IsNullOrEmpty(urlTextBox.Text) ||
                !System.Uri.IsWellFormedUriString(urlTextBox.Text.Trim(), UriKind.Absolute))
            {
                outputTextBlock.Text = "a valid URL is not provided, so request will be created but will not be submitted.";
            }
            else
            {
                url = urlTextBox.Text.Trim();
            }

            // create certificate request
            try
            {
                // call the default constructor of CertificateRequestProperties
                CertificateRequestProperties reqProp = new CertificateRequestProperties();

                reqProp.Subject      = "Toby";
                reqProp.FriendlyName = "Toby's Cert";

                if (true == userStoreCheckBox.IsChecked)
                {
                    // have to use User's Certificate Store
                    // call User Certificate Enrollment function createRequest to create a certificate request
                    certificateRequest = await CertificateEnrollmentManager.UserCertificateEnrollmentManager.CreateRequestAsync(reqProp);

                    outputTextBlock.Text += "\nRequest created, content:\n" + certificateRequest;
                }
                else
                {
                    // use App's certificate store
                    // call Certificate Enrollment function createRequest to create a certificate request
                    certificateRequest = await CertificateEnrollmentManager.CreateRequestAsync(reqProp);

                    outputTextBlock.Text += "\nRequest created, content:\n" + certificateRequest;
                }
            }
            catch (Exception ex)
            {
                outputTextBlock.Text += "\nCertificate request creation failed with error: " + ex.Message;
            }

            // make sure request was created successfully and there is a valid URL
            if (String.IsNullOrEmpty(certificateRequest) ||
                string.IsNullOrEmpty(url))
            {
                return;
            }

            // submit request for enrollment and install the certificate
            try
            {
                outputTextBlock.Text += "\n\nSubmitting request to server...";
                response              = await SubmitCertificateRequestAndGetResponse(certificateRequest, url);

                if (response.StartsWith("Error:", StringComparison.OrdinalIgnoreCase))
                {
                    // there was an error getting the response from server. Display the error
                    outputTextBlock.Text += "\n" + response;
                }
                else
                {
                    outputTextBlock.Text += "\nResponse received, content: \n" + response;

                    // install certificate
                    outputTextBlock.Text += "\nInstalling certificate ...";

                    if (true == userStoreCheckBox.IsChecked)
                    {
                        // install certificate to User's certificate store
                        // call User Certificate Enrollment InstallCertificate function to install certificate
                        await CertificateEnrollmentManager.UserCertificateEnrollmentManager.InstallCertificateAsync(response, InstallOptions.None);

                        outputTextBlock.Text += "\nThe certificate response is installed successfully to User's certificate store.\n";
                    }
                    else
                    {
                        // install certificate to App's certificate store
                        // call Certificate Enrollment InstallCertificate function to install certificate
                        await CertificateEnrollmentManager.InstallCertificateAsync(response, InstallOptions.None);

                        outputTextBlock.Text += "\nThe certificate response is installed successfully to App's certificate store.\n";
                    }
                }
            }
            catch (Exception ex)
            {
                outputTextBlock.Text += "\nCertificate Installation failed with error: " + ex.Message + "\n";
            }
        }