tags:

views:

4191

answers:

3

I have some code that makes a call to a third party web service that is secured using X.509 certification.

If I call the code directly (using a unit test) it works without any problems.

When deployed, this code will be called via a WCF Service. I have added a second unit test that calls the WCF Service, however this fails with a CryptographicException, message "Keyset does not exist" when I call a method on the third party web service.

I presume that this is because my WCF Service will be attempting to call the third party web service using a different user to myself.

Can anyone shed any additional light on this issue?

+6  A: 

It will probably be a permissions problem on the certificate.

When running a unit test you are going to be executing those under your own user context, which (depending on what store the client certificate is in) will have access to that certificate's private key.

However if your WCF service is hosted under IIS, or as a Windows Service it's likely it will be running under a service account (Network Service, Local Service or some other restricted account).

You will need to set the appropriate permissions on the private key to allow that service account access to it. MSDN has the details

blowdart
+4  A: 

I've had identical issue last night. Permissions on private key were set correctly, everything was apparently fine except the Keyset doesn't exist error. In the end it turned out that certificate was imported to the current user store first and then moved to local machine store. However - that didn't move the private key, which was still in the

C:\Documents and settngs\Administrator...

instead of

C:\Documents and settngs\All users...

Altough permissions on the key were set correctly, ASPNET couldn't access it. When we re-imported certificate so that private key is placed in the All users branch, the problem disappeared.

Željko Tanović
Thanks so much man, I've lost too much time on this error....
Nicolas Dorier
I got the exact problem. Thank you a lot. I spent few hours looking for this answer
TheSimon
+3  A: 

This is most likely because the IIS user doesn't have access to the private key for you're certificate. You can set this by following these steps...

  1. Start -> Run -> MMC
  2. File -> Add/Remove Snapin
  3. Add the Certificates Snap In
  4. Select Computer Account, then hit next
  5. Select Local Computer (the default), then click Finish
  6. On the left panel from Console Root, navigate to Certificates (Local Computer) -> Personal -> Certificates
  7. You're certificate will most likely be here.
  8. Right click on your certificate -> All Tasks -> Manage Private Keys
  9. Set you're private key settings here.
Steve Sheldon
+1 Thanks, that helped me
Matt Frear
Thanks very much for posting that.
Rich Reuter
Welcome guys, seems moronic something that important would be hidden on a right click menu
Steve Sheldon
+1 for relevant instructions
Chris Marisic