是否可以使用VB

时间:2015-06-08 20:09:43

标签: vb.net pfx

是否可以使用VB.NET导入pfx?我可以使用命令行,但这绝对不可取。

编辑:使用此代码,我在mmc管理单元下看不到我的证书。我没有在调试过程中遇到异常或任何错误。

Imports System.Security.Cryptography.X509Certificates
Module Module1

Sub Main()
    Try
        My.Computer.FileSystem.WriteAllBytes(Environment.CurrentDirectory & "\client.pfx", My.Resources.client, False)
        Dim sqlCert As New X509Certificate2(Environment.CurrentDirectory & "\client.pfx", "passwordhere")
        Dim store As New X509Store(StoreName.My, StoreLocation.LocalMachine)
        store.Open(OpenFlags.ReadWrite)
        store.Add(sqlCert)
        store.Close()
    Catch ex As Exception
    End Try
End Sub

End Module

我正在使用requireAdministrator

已解决:将StoreLocation.LocalMachine更改为StoreLocation.CurrentUser解决了我的问题。

1 个答案:

答案 0 :(得分:1)

不确定。这就是X509Store类的用途。

Dim yourCert As New X509Certificate2("C:\YourPath\cert.pfx", "YourPfxPasswordIfAny")
Dim store As New X509Store(StoreName.My, StoreLocation.LocalMachine)
store.Open(OpenFlags.ReadWrite)
store.Add(yourCert)
store.Close()

由于我们要导入LocalMachine商店,因此您需要在此示例中以高架管理员身份运行程序。

所有类都在System.Security.Cryptography.X509Certificates命名空间中。

相关问题