How do I show and select one of the digital certificates on my computer

Asked

Viewed 3,519 times

2

In my desktop I have some certificates and I need to select one of them, I would like to know via VN.Net or C# code how to appear a list and I choose the digital certificate I need, without having to assign this list of certificates in a combobox.

Code

    Dim ListaCertificados As New List(Of X509Certificate2)()
Private Sub ReloadCerts()

    Dim Store As X509Store = New X509Store(StoreName.My, StoreLocation.CurrentUser)
    Store.Open(OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly)

    Dim _certs As X509Certificate2Collection = Store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, True).Find(X509FindType.FindByKeyUsage, X509KeyUsageFlags.DigitalSignature, True)

    For Each Certificado In _certs

        If Certificado.HasPrivateKey Then

            ListaCertificados.Add(Certificado)
            Certificado.GetNameInfo(X509NameType.SimpleName, False)

        End If
    Next
End Sub
  • Does this help you? http://stackoverflow.com/q/1205295/221800

  • In a way, yes, but I didn’t understand X509Certificate2UI, he presents me with a mistake of not being declared, but has already been declared.

  • He’s probably not visible in that scope. I wouldn’t know without details.

  • Here is an example: https://msdn.microsoft.com/pt-br/library/system.security.cryptography.x509certificates.x509certificate2ui(v=vs.90). aspx? Cs-save-lang=1&Cs-lang=Vb#code-snippet-2 ...

  • You put using System.Security.Cryptography.X509Certificates;?

  • Both this using System.Security.Cryptography.X509Certificates; how much this using System.Security.Cryptography; And nothing wrong. 'X509Certificate2UI' não está declarado. Ele pode ser inacessível devido ao seu nível de proteção.

  • I managed to solve the problem, I had to add the reference System.Security.dll

  • Of course, I forgot this, I thought it was already referenced. And it solved everything? If I put as answer, answers your question?

  • So, this was an unforeseen. The answer to my question is the following code, in which case I got what I wanted. Dim Store As X509Store = New X509Store(StoreName.My, StoreLocation.CurrentUser) Store.Open(OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly) Dim certificateCollection As X509CertificateCollection = X509Certificate2UI.SelectFromCollection(Store.Certificates, "Caption", "Message", X509SelectionFlag.SingleSelection)

  • Post the answer you then.

  • I will not post, because the merit was yours in showing the link in the first comment, than in the case, helped me with what I was looking for. I thank!

Show 6 more comments

1 answer

4


Based in that reply in the OS it is possible to do what you want. The search way is that it has to be different (taking advantage of the code placed in the comment would look like this:

Dim certificateCollection As X509CertificateCollection = X509Certificate2UI.SelectFromCollection(Store.Certificates, "Caption", "Message", X509SelectionFlag.SingleSelection)

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.