8

I have application and I have to use a certificate which requires a pin from prompt window.

I have 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 console application but when I run that code in windows service or console application started from task scheduler then application freezes on that line.

certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters);

No exceptions, no progress.

I'm running windows service with the same credentials as an application.

Windows 10 / Windows Server 2012

Do you have any ideas what is wrong?

Degusto
  • 291
  • 2
  • 10
  • You could try to run service as local system and start `Interactive Services Detection` service (UI0Detect). If there is a PIN prompt waiting it should take you to special desktop 0 where you can enter the pin. – pepo Jan 24 '17 at 19:42
  • I tried but nothing happened. I'll try again in a next week. – Degusto Jan 27 '17 at 17:50

2 Answers2

1

Ok, after a break I have found solution.

I had to add impersonation around this line:

certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters);

I used the same credentials like in my service and set LogonType as Interactive.

Degusto
  • 291
  • 2
  • 10
0

The problem is that PIN prompt appears in the service account's desktop which is not exposed to console user (even if both run under the same account). The service doesn't hang, it waits for PIN input and will never receive it.

Crypt32
  • 12,850
  • 2
  • 41
  • 70
  • But in console application I can replace private key and prompt doesn't appear. Without replacing private key I have prompt in console application. – Degusto Jan 24 '17 at 19:12