How to retrieve all certificates in your X509Store

Tagged:
Freelance Jobs

I am using the following code to retrieve all certificates in my PC from an asp.net webapp. The certificates collection is empty, and I can't understand why.

I tried impersonating my own user account and I didn't succeed as well. What am I doing wrong?

var store = new X509Store(StoreLocation.CurrentUser); //StoreLocation.LocalMachine fails too
var certificates = store.Certificates;
foreach (var certificate in certificates)
{
    var friendlyName = certificate.FriendlyName;
    Console.WriteLine(friendlyName);
}

//original problem: fetch a single certificate by its subject name
X509Certificate2 clientCertificate = CertificateUtility.GetCertificate(StoreName.My, StoreLocation.CurrentUser,  "CN=mypc.domainname"); //returns null :(

pabloH
2011-01-18 21:33:24 Scores:8

4 answers

Answer 1
Scores:9

Add this line of code to the second line and see how it works:

store.Open(OpenFlags.ReadOnly);

and then this at the bottom :):

store.Close();
Chris B. Behrens H
2011-01-18 21:40:45
I also use this to open the store: store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
rick schott H
2011-01-18 21:44:13 Scores:0
Answer 2
Scores:2

all in one ...

i have an apache server (xamp) with https, i acess through https and c# (vs2010) to a php upload page

1) install de certificate from i.e in the personal folder certificate, for example.

2) to view the certicates run "certmgr.msc" , at least in win7

## listing the personal certificates
var store = new X509Store(StoreLocation.CurrentUser); 

store.Open(OpenFlags.ReadOnly); 

var certificates = store.Certificates;
foreach (var certificate in certificates)
{
    var friendlyName = certificate.FriendlyName;
    var xname = certificate.GetName(); //obsolete
    Console.WriteLine(friendlyName);
}

store.Close();
## find specific certificate
string certificateName = "CN=localhost"; //name found in the var xname
X509Store storex = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                    storex.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificatesx =
            storex.Certificates.Find(X509FindType.FindBySubjectName, 
            certificateName,
            true);

X509Certificate certificatex = certificates[0];

storex.Close();
cealex H
2011-09-09 16:48:18
Answer 3
Scores:1

Look in your certificate store(mmc/add/certificate snap-in/my user account/Certificates - Current User/Personal/Certificates) to see the subject name to make sure "CN=mypc.domainname" is whats actually on the cert.

"CN=mypc.domainname"

vs

"CN = mypc.domainname"

...etc

rick schott H
2011-01-18 21:39:43
Yes, I just didn't want to expose my real pc/domain name here. ;)
pablo H
2011-01-18 21:45:59 Scores:0
I know that's not the real CN name you have, just want you to make sure your "mypc.domainame", whatever it really is, is whats on your cert.
rick schott H
2011-01-18 21:47:51 Scores:0
Answer 4
Scores:0

I can find certificates by ...

var certificateStore = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);

certificateStore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

var certificateCollection = certificateStore.Certificates.Find(X509FindType.FindBySubjectName, "mycert.me.com",false);

certificateStore.Close();

var certificate = certificateCollection[0];

certificateCollection will have the certificates I care about ... if it is just one then I get first element in the collection.

Nabheet Sandhu H
2011-08-17 19:20:18
Share |
View original post at stackoverflow.com

Related topics

how to authenicate clients using certificates in an ASP.NET web service

I need to know how the a .net web service authenticates a request from a client that is using a certificate. I know that the client will have to attach their certificate to the service proxy before they make a request, but I dont know how to check that the ...

Need some help with HTTPS, certificates and signing

I am quite new to HTTPS and can not get my head around it. Can someone suggest good starting point for learning about HTTPS, certificates and signing? Possibly with a working example in ASP.NET with IIS. ...

Authenticating SQL connection using certificates in .NET

I've noticed that it is possible SQL Server 2005/2008 to authenticate replication accounts using certificates. Is it possible to authenticate .NET SqlConnection in the same manor? Ideally, I'd like to do away with password authentication completely and have the aspnet user connect using a certificate stored against its account. Is this ...

confusion about Certificates

I have WCF REST web service hosted by IIS, it works on HTTPS, I generate Certificate on IIS and assign Https to a port I generate cer through IE browser. I create a test application and regardless Add a client certificate or not or even add a wrong certificate the connection ...

How to create Self signed certificates

I am trying to open my homepage with HTTPS in my test server. Is there a way i can create Self signed certifcates and see if https works. Please suggest me some links or any ideas with how to do!! Its an ASP.Net project and IIS V6.0 I appreciate all your ...

.NET certificates (C#)

My website is doing some http posts to another server and I need to attach a certificate. I am using this code to open the certificate store and getting the certificate I need: X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); X509CertificateCollection certCollection = store.Certificates; The user is a domain account and the application pool for ...

How to retrieve certificates from a pfx file with c#?

I've been googling around for half a day looking for a way to read a pfx file and import the certifcates into the certstore. So far, i am able to read the pfx file with X509Certifcate and able to import one certificate within the pfx file. So far so good, ...

PowerShell - X509Certificates.X509Store get all certificates?

I want to get all certificates from my system. So I used the System.Security.Cryptography.X509Certificates class. When I remove the () after the X509Store I getting the same results like I entered "My" What is the right membername to see all certificates? It is possible? MSDN StoreName Enumeration $store=new-object System.Security.Cryptography.X509Certificates.X509Store("CA") # Put in CA, My, ...

How to operate with X509 certificates in .NET code

I have some code that needs to work with X509 Certificate information. I have downloaded a sample that does this: const string CertWithoutPrivateKey = "MII...."; const string CertWithPrivateKey = "MII..."; public static SecurityToken GetSigningToken(bool includePrivateKey) { X509Certificate2 cert = null; if (includePrivateKey) { ...

Can't read CurrentUser certificates from X509Store

I'm developing ASP.NET 4.0 web application, and I want to read the current user certificates from X509Store. Reading the LocalMachine certificates works fine, but if I set the StoreLocation to CurrentUser, it gives me an empty collection. The following code works fine : X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); // StoreLocation.CurrentUser store.Open(OpenFlags.ReadOnly | ...