How to select a Certificate using the Windows Security's Confirm Certificate popup in C# console or WinForms application?

Viewed 1888

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.

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 Security Confirm Certificate dialog box

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?

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 screenshot

1 Answers

You can try using X509Certificate2UI.SelectFromCollection which is documented here.

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);
Related