Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created June 5, 2023 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/b02de6a85c73a93f69b877f991b75536 to your computer and use it in GitHub Desktop.
Save bjoerntx/b02de6a85c73a93f69b877f991b75536 to your computer and use it in GitHub Desktop.
static X509Certificate2 RetrieveCertificate() {
// get and open certificate store for current user
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
// retrieve the certificate using the integrated Windows UI
X509Certificate2Collection selectedCertificates =
X509Certificate2UI.SelectFromCollection(
store.Certificates,
"Choose your certificate",
"Please select a certificate that is used to sign the PDF.",
X509SelectionFlag.SingleSelection);
// return the first selected certificate with a private key
foreach (var certificate in selectedCertificates) {
if (certificate.HasPrivateKey)
return certificate;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment