无法以编程方式检索通过mmc为服务帐户安装的证书

时间:2020-06-25 19:24:56

标签: .net vb.net windows-services windows-server-2008 x509certificate2

我正在尝试将应用程序转换为服务(.NET)。该服务必须通过Web服务(FileNet P8 .NET凭据系统)登录远程服务器。

但是,URL是 HTTPS ,因此它需要证书。

我跟随Microsoft's instructions为MMC的服务帐户安装了证书。证书(自签名)安装在受信任的根证书颁发机构证书库中。 (免责声明:我还不太了解MMC的工作原理,尤其是在进行更改时有效)。

该证书位于HKLM\SOFTWARE\Microsoft\Cryptography\ Services\<ServiceName>\SystemCertificates\Root\Certificates\<digitalFootprint>中。

但是,这段代码没有检索证书:

' Environment.UserName = SERVICE LOCAL (french)
LOGGER.info("Certifs for " & Environment.UserName)
Dim store As X509Certificates.X509Store =
    New X509Certificates.X509Store(X509Certificates.StoreName.Root,
                                   X509Certificates.StoreLocation.CurrentUser)
store.Open(X509Certificates.OpenFlags.ReadOnly)
For Each certificate As X509Certificates.X509Certificate2 In store.Certificates
    LOGGER.info(certificate.Issuer & ": " & 
                If(certificate.Verify, "Valid", "Invalid"))
Next 
LOGGER.info("End Certifs")

当然,下面的测试代码会失败:

Dim wq As Net.WebRequest = Net.WebRequest.Create("https://<URL>")
Dim reader As IO.StreamReader = New IO.StreamReader(wq.GetResponse.GetResponseStream)
LOG.info(reader.ReadToEnd)

但是,如果我使用具有证书的用户帐户运行该服务,则该服务将运行。我做错了什么?

其他问题:是否可以通过服务帐户运行Internet Explorer来执行证书安装? (作为解决方法)

2 个答案:

答案 0 :(得分:1)

.NET不支持在服务帐户存储中查找证书。基础API CertOpenStore function通过CERT_SYSTEM_STORE_CURRENT_SERVICECERT_SYSTEM_STORE_SERVICES值支持服务帐户存储。 .NET仅支持StoreLocation枚举中的CERT_SYSTEM_STORE_LOCAL_MACHINECERT_SYSTEM_STORE_CURRENT_USER值。

MMC使用本机API,因此您可以从MMC访问服务证书存储。这是.NET中的一种已知限制。解决此问题的唯一方法是直接使用CertOpenStore函数(通过p / invoke)。

答案 1 :(得分:0)

一旦我也遇到过类似的行为。.让我们看看下面的代码是否可以帮助您,请记住我们的证书是个人证书:

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certificates = store.Certificates.Find(X509FindType.FindByIssuerName, "test.supportxxxx@test.com", true);
if (certificates.Count > 0)
 {
     // certificate found            
 }