如何在 C# 控制台或 WinForms 应用程序中使用 Windows 安全中心的确认证书弹出窗口选择证书? [英] How to select a Certificate using the Windows Security's Confirm Certificate popup in C# console or WinForms application?
问题描述
我想编写一个 C# 控制台或 WinForms 应用程序,以使用证书 .PFX 文件向 PDF 文件添加数字签名.目前,我可以使用 iText7 和 BouncyCastle 库,使用文件系统文件夹中的 PFX 证书对 PDF 文件进行签名.
I want to write a C# console or WinForms application to add digital signature to a PDF file using a Certificate .PFX file. Currently, I am able to sign PDF files using a PFX certificate from a folder in the file system, using iText7 and BouncyCastle libraries.
但我需要让用户在运行时从系统中已安装的证书列表中选择一个证书.
But I need to ask the user to choose a Certificate at run time from the list of installed certificates in the system.
具体来说,我想显示 Windows 安全中心的确认证书对话框,并使用用户选择的证书进行签名.我想显示如下所示的弹出窗口:
Specifically, I want to display the Windows Security's Confirm Certificate dialog box, and sign using the certificate selected by the user. I want to show the popup shown below:
我的具体问题是:如何触发 Windows 安全中心的确认证书"对话框,以及如何读取用户选择的证书?
My specific question is: How can I trigger the Windows Security's Confirm Certificate dialog box, and how can I read the certificate selected by the user?
编辑 1:如果我使用 X509Certificate2UI.SelectFromCollection 方法,我会得到一个不同的弹出窗口,如下所示.这和我想要的不一样.
EDIT 1: If I use X509Certificate2UI.SelectFromCollection method, I get a different popup as shown below. This is different from what I want.
推荐答案
您可以尝试使用 X509Certificate2UI.SelectFromCollection
文档中的 此处.
You can try using X509Certificate2UI.SelectFromCollection
which is documented here.
正如@Crypt32 在评论中提到的,我觉得有必要添加代码示例如何使用 api.我想知道@AllSolutions 如何使用相同的 API 设法获得该 UI 窗口.似乎 UI 随 selectionFlag
参数而变化.
As @Crypt32 mentioned in the comments i felt the neccessity of adding code example how to use the api. I was wondering how @AllSolutions managed to get that UI window using the same API. It seems the UI changes with the selectionFlag
parameter.
var store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
var collection = store.Certificates;
var cert = X509Certificate2UI.SelectFromCollection(collection, "Select", "Select a certificate to sign", X509SelectionFlag.SingleSelection);
这篇关于如何在 C# 控制台或 WinForms 应用程序中使用 Windows 安全中心的确认证书弹出窗口选择证书?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!