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?
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.