使用 VB.NET 安装 x509 证书 [英] x509 Certificate installation using VB.NET

查看:24
本文介绍了使用 VB.NET 安装 x509 证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过 vb.net 在系统中安装 x509 证书时遇到问题.

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

我错过了什么吗?

推荐答案

使用 X509Certificate2Collection 类如下:

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.

请注意 CA 证书的正确位置是 Trusted Root Certification Authorities 文件夹.如果您信任颁发者,您应该只在那里导入证书.

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.

这篇关于使用 VB.NET 安装 x509 证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆