Private key setting in certificate depends on Windows service

I have an application, and I must use a certificate that requires output from the prompt window.

I have the following code.

SecureString password = GetPassword(); X509Certificate2 certificate = GetCertificate(); var cspParameters = new CspParameters(1, "ProviderName", "KeyContainerName", null, password); certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters); 

Everything works fine in a console application, but when I run this code in a Windows service or a console application launched from the task scheduler, then the application freezes on this line.

 certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters); 

No exceptions, no progress.

I am running the Windows service with the same credentials as the application.

Windows 10 / Windows Server 2012

Do you have any ideas what is wrong?

+8
c # windows-services x509certificate2 private-key
source share
2 answers

Well, after the break, I found a solution.

I had to add an impersonation around this line:

 certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters); 

I used the same credentials as in my service and installed LogonType as interactive.

+1
source share

The problem is that on the desktop of the service account, a PIN code request is displayed that is not displayed to the console user (even if both of them work under the same account). The service does not hang, it waits for a PIN code and will never receive it.

0
source share

All Articles