Get List of Certificates from the Certificate Store in C#

Get List of Certificate Store Names in C#

http://msdn.microsoft.com/en-us/library/aa376058(VS.85).aspx

Don't think there's a managed .net way of doing this. Possibly the closest may be to use .net's registry functions to read the store names from the registry?

Get list of X509Certificate from cert store C# MVC

Most of the time, you want to check the machine store certs, not the ones for your current user. To do that:

X509Store store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
foreach (X509Certificate2 certificate in store.Certificates)
{
// TODO
}

This gives you a consistent list, regardless of the IIS user.

Get all certificates installed on Local machine

Certificates on your machine stored in a different stores, so you need open all of them. Please see that MSDN
article.

Code example:

public class CertDetails
{
public string Name { get; set; }
public string HasPrivateKey { get; set; }
public string Location { get; set; }
public string Issuer { get; set; }
}

// stores and they friendly names
var stores = new Dictionary<StoreName, string>()
{
{StoreName.My, "Personal"},
{StoreName.Root, "Trusted roots"},
{StoreName.TrustedPublisher, "Trusted publishers"}
// and so on
}.Select(s => new {store = new X509Store(s.Key, StoreLocation.LocalMachine), location = s.Value}).ToArray();

foreach (var store in stores)
store.store.Open(OpenFlags.ReadOnly); // open each store

var list = stores.SelectMany(s => s.store.Certificates.Cast<X509Certificate2>()
.Select(mCert => new CertDetails
{
HasPrivateKey = mCert.HasPrivateKey ? "Yes" : "No",
Name = mCert.FriendlyName,
Location = s.location,
Issuer = mCert.Issuer
})).ToList();

locating a specific certificate from the Windows certificate store (C# - ASP.Net)

After considering the suggestions from @Crypt32, I moved my tokens to where the Application will be hosted, so this way I'm not looking on the end user's machine for a certificate, but instead it will be locally stored at the server hosting the application. To search for the token, I use the exact same code up there in the question with a very slight edit:

X509Store st0re = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
st0re.Open(OpenFlags.ReadOnly);
count = st0re.Certificates.Count; //Count the certificates in the store
X509Certificate2Collection certs = st0re.Certificates.Find(
X509FindType.FindBySubjectDistinguishedName,
"C=US, S=WA, L=Redmond, O=Microsoft Corporation, OU=Web Services",
true);
st0re.Close();

Output = certs[0].ToString(); // = count.ToString()

All I did was to replace X509FindType.FindBySubjectName with X509FindType.FindBySubjectDistinguishedName
In this case, all elements in the certificate subject have to be listed in that exact format.

How to retrieve all certificates in your X509Store

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();

How to get all certificates with powershell?

There is a PSDrive Cert, which contains CurrentUser and LocalMachine.

So this get you all certificates:

Get-ChildItem Cert:\ -Recurse


Related Topics



Leave a reply



Submit