x509 Certificate installation using VB.NET

十年热恋 提交于 2019-12-08 08:01:49

问题


I have an issue when installing x509 certificate in the system through vb.net.

Installation itself is successful, but when I do install it through the code I do get one entry in Certificate Management window as displayed bellow:

However when I install it manually using import function in Certificate Management window I do get two entries in the list for this certificate:

The problem that I am facing is that when I use this certificate to perform certain tasks (passing some info to the third party service) it only works when it is manually imported (there are two entries in the certificate list). It looks like when installing certificate through the code, it doesn't install it fully. I done lots of research on code used to install certificate and it looks fairly straight forward:

    With ofd
            .Title = "Select Certificate"
            .FileName = ""
            .CheckFileExists = True
            If .ShowDialog <> Windows.Forms.DialogResult.Cancel Then
                Dim cert As New X509Certificate2(.FileName, "xxxxxxx", X509KeyStorageFlags.UserKeySet)
                Dim certStore As New X509Store(StoreName.My, StoreLocation.CurrentUser)
                certStore.Open(OpenFlags.ReadWrite)
                certStore.Add(cert)
                certStore.Close()
            End If
        End With

Am I missing something?


回答1:


Use the X509Certificate2Collection class like this:

Dim collection = New X509Certificate2Collection()

collection.Import(.FileName, "xxxxxxx", X509KeyStorageFlags.UserKeySet)

Dim store = New X509Store(StoreName.My, StoreLocation.CurrentUser)

store.Open(OpenFlags.ReadWrite)

Try
    For Each certificate As X509Certificate2 In collection
        store.Add(certificate)
    Next
Finally
    store.Close()
End Try

This allows you to import all the certificates from the file.

Please note that the right place for CA certificates is the Trusted Root Certification Authorities folder. And you only should import certificates there if you trust the issuer.



来源:https://stackoverflow.com/questions/33899509/x509-certificate-installation-using-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!