Пример #1
0
        private static unsafe void DisplayX509Certificate(X509Certificate2 certificate, IntPtr hwndParent)
        {
            using (SafeCertContextHandle safeCertContext = X509Utils.DuplicateCertificateContext(certificate))
            {
                if (safeCertContext.IsInvalid)
                {
                    throw new CryptographicException(SR.Format(SR.Cryptography_InvalidHandle, nameof(safeCertContext)));
                }

                int dwErrorCode = ERROR_SUCCESS;

                // Initialize view structure.
                Interop.CryptUI.CRYPTUI_VIEWCERTIFICATE_STRUCTW ViewInfo = default;
#if NET7_0_OR_GREATER
                ViewInfo.dwSize = (uint)sizeof(Interop.CryptUI.CRYPTUI_VIEWCERTIFICATE_STRUCTW.Marshaller.Native);
#else
                ViewInfo.dwSize = (uint)Marshal.SizeOf <Interop.CryptUI.CRYPTUI_VIEWCERTIFICATE_STRUCTW>();
#endif
                ViewInfo.hwndParent         = hwndParent;
                ViewInfo.dwFlags            = 0;
                ViewInfo.szTitle            = null;
                ViewInfo.pCertContext       = safeCertContext.DangerousGetHandle();
                ViewInfo.rgszPurposes       = IntPtr.Zero;
                ViewInfo.cPurposes          = 0;
                ViewInfo.pCryptProviderData = IntPtr.Zero;
                ViewInfo.fpCryptProviderDataTrustedUsage = false;
                ViewInfo.idxSigner        = 0;
                ViewInfo.idxCert          = 0;
                ViewInfo.fCounterSigner   = false;
                ViewInfo.idxCounterSigner = 0;
                ViewInfo.cStores          = 0;
                ViewInfo.rghStores        = IntPtr.Zero;
                ViewInfo.cPropSheetPages  = 0;
                ViewInfo.rgPropSheetPages = IntPtr.Zero;
                ViewInfo.nStartPage       = 0;

                // View the certificate
                if (!Interop.CryptUI.CryptUIDlgViewCertificateW(ViewInfo, IntPtr.Zero))
                {
                    dwErrorCode = Marshal.GetLastWin32Error();
                }

                // CryptUIDlgViewCertificateW returns ERROR_CANCELLED if the user closes
                // the window through the x button or by pressing CANCEL, so ignore this error code
                if (dwErrorCode != ERROR_SUCCESS && dwErrorCode != ERROR_CANCELLED)
                {
                    throw new CryptographicException(dwErrorCode);
                }
            }
        }