0

I am trying to find where the certificate is stored on my local machine and then as well as our dev servers. I can go Run -> MMC -> File - > Add/Remove SnapIns and select certificates and Current User and see my personal certificates. However, I am trying to utilize this code for an HttpWebRequest and I cannot find the url.

string certPath = @"e:\mycertificate.cer"; //This Value
X509Certificate myCert = X509Certificate.CreateFromCertFile(certPath);
request.ClientCertificates.Add(myCert);

In another area we set up a proxy and do it like this.

proxy.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, CertificateName);

So obviously a little different implementation and I am unsure as to where/how to find the location to fill in for the first example.

Solution that worked for me

public WebRequest GetWebRequest(string address)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
    X509Certificate myCert = null;
    X509Store store = new X509Store("My");

    store.Open(OpenFlags.ReadOnly);

    foreach (X509Certificate2 mCert in store.Certificates)
    {
        if (mCert.FriendlyName.Contains("certname"))
        {
            myCert = mCert;
        }
    }

    if (myCert != null) { request.ClientCertificates.Add(myCert); }

    return request;
}
Adam
  • 3,615
  • 6
  • 32
  • 51

1 Answers1

2

Assuming like you want to pick a certificate somehow and not really care if it is from file or not. In this case you can use certificate store object and find one you need (i.e. by thumbprint). Check out this Get list of certificates from the certificate store in C# and MSDN article on X509Store.Certificates which contains sample too:

X509Store store = new X509Store("My");
store.Open(OpenFlags.ReadOnly);

foreach (X509Certificate2 mCert in store.Certificates){
    //TODO's
}
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Maybe I am missing something I am just not able to find the path to the certificate – Adam May 25 '12 at 16:53
  • "It is hard to find black cat in dark room, especially if it is not there"... Certificates installed in storage **do not have** physical files associated with them. If you need to load from file - bring file (and potentially password) yourself somehow. It looked from your usage example that you don't have to load from file (`Request.ClientCertificates.Add`)... – Alexei Levenkov May 25 '12 at 16:58