0

I am impersonating a service user account in order to connect to a webservice that requires a cert to connect. I have installed the client cert on the service account on the machine which is running the code however I receive the error System.Security.Cryptography.CryptographicException: The system cannot find the file specified.

 using (var ctx = new ImpersonationContext("svcAcctUserName", "domain", "password"))
 {
    var clientCert = new X509Certificate2("filePath", "certPassword");
 }

The impersonation code works, for brevity I have left it out but I check to make sure my context is switched to the svcAcctUserName user by logging the Environment.UserName, which shows that I am running as svcAcctUserName. The filePath is correct, again I left it out, but I open and close the file before I create the X509Certificate2 object to make sure I have both access to the file and that my path is correct.

The error is confusing since I provide the path as a parameter and I know for certain the user running the code has access.

EDIT:

Also tried to do this: How to call a Web service by using a client certificate for authentication in an ASP.NET Web application

Although I am not using an asp.net application, I gave it a try anyway. I added the certificates add-in to the mmc, added the "local computer" certificates add in and then imported the cert into the Personal store of the local machine.

I then ran:

WinHttpCertCfg.exe -g -c LOCAL_MACHINE\My -s issuedToName -a domain\svcAcctUserName

Tried running the operation again, still same problem.

What am I missing?

4
  • How are you accessing the webservice? Is the certificate installed in the keystore or just located in the filesystem?
    – Brian
    May 31, 2012 at 21:47
  • It's in both the personal key store of the impersonated service account and in the local_machines key store with permission granted to access it via the impersonated user. See below for what I tried.
    – Bitfiddler
    May 31, 2012 at 22:43
  • Your sample show call to constructor for X509Certificate2 that uses file path, but you are talking about cert in the store... Please align your question and sample code to talk about the same thing. May 31, 2012 at 23:08
  • 1
    Alex, your comment is not helpful. If you have identified a misunderstanding on my part of how the cert system works. Let me know. Asking me to fix something that I am not understanding is not the point of this forum.
    – Bitfiddler
    May 31, 2012 at 23:23

1 Answer 1

2

So, as Alex pointed out, I do not understand the underlying architecture of certificate system in windows. However, after performing the above steps and modifying my code to use the X509Store, I have it working. Hopefully this will help someone:

using (var ctx = new ImpersonationContext("svcAcctUserName", "domain", "password"))
{
   var store = new X509Store(StoreLocation.LocalMachine);
   store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
   var clientCert = store.Certificates.Find(X509FindType.FindByIssuerName, "IssuerNameHere", false);
   var clientCert2 = new X509Certificate2(clientCert[0]);
}
4
  • +1 for posting the solution (feel free to accept it - ok to do so). Notes: there are very relaxed restrictions on size of your original question (not comments), so put you question updates there. You can edit your question freely even with 1 reputation. Putting code in comments would have been much worse... When you "answered" your own question with your previous comment you essentially made a lot of people to skip you "answered" question thus lowering chances to get an answer. May 31, 2012 at 23:39
  • I can't accept my own answer for another 2 days unfortunately. However I find that when people continually edit their original question in response to comments, it makes it difficult to follow for people who later are trying to follow the flow of the solution. I for one prefer to know how people arrived at a solution, the journey many times is as instructive as the answer, but when you have a question that's been edited 5 times in response to comments its difficult to reverse engineer the order of what happened.
    – Bitfiddler
    May 31, 2012 at 23:46
  • Also, I don't pay attention at all to the point system so I'm not particularly concerned if someone doesn't mark my answer correctly. If my solution gets read and it helps someone... great. Regardless of whether or not I get credit (although credit is welcome).
    – Bitfiddler
    May 31, 2012 at 23:47
  • Good point. Note that accepting an answer is important for other people who would read it in a future - it shows that that the particular approach worked/helped at least to poster - so do it for future generations :). Jun 1, 2012 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.