Search code examples
c#windowsmodel-view-controllerx509certificatex509certificate2

Get list of X509Certificate from cert store C# MVC


I'm trying to get the list of certificates from cert store. This is the code I'm using from this post Get list of certificates from the certificate store in C#:

X509Store store = new X509Store(StoreName.My);
store.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 mCert in store.Certificates)
{
  // TODO
}

When I run this code from Test Explorer is finding all available certificates, but when I run it on my MVC application is not returning any certificate. I'm running VS 2013 as administrator.

Could you please address me what I'm doing wrong?

EDIT:

When I'm running the code on IIS Express I'm getting the list of certificates, but when I run it on Local IIS I'm not getting any results.

Regards,


Solution

  • 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.