在证书中设置私钥会挂起Windows服务

Deg*_*sto 8 c# windows-services x509certificate2 private-key

我有应用程序,我必须使用一个证书,需要一个来自提示窗口的引脚.

我有以下代码.

SecureString password = GetPassword();
X509Certificate2 certificate = GetCertificate();

var cspParameters = new CspParameters(1,
                                     "ProviderName",
                                     "KeyContainerName",
                                     null,
                                     password);

certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters);
Run Code Online (Sandbox Code Playgroud)

在控制台应用程序中一切正常,但是当我在Windows服务或从任务调度程序启动的控制台应用程序中运行该代码时,应用程序在该行上冻结.

certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters);
Run Code Online (Sandbox Code Playgroud)

没有例外,没有进展.

我正在使用与应用程序相同的凭据运行Windows服务.

Windows 10/Windows Server 2012

你有什么想法有什么不对吗?

Deg*_*sto 1

好的,休息后我找到了解决方案。

我必须在这一行添加模拟:

certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters);
Run Code Online (Sandbox Code Playgroud)

我使用了与我的服务中相同的凭据,并将 LogonType 设置为 Interactive。