Skip to content

Instantly share code, notes, and snippets.

@devscott
Created October 12, 2015 15:28
Show Gist options
  • Save devscott/550ccaec05b9c75bb8df to your computer and use it in GitHub Desktop.
Save devscott/550ccaec05b9c75bb8df to your computer and use it in GitHub Desktop.
Listing and searching the Certificate Store
using System;
using System.Security.Cryptography.X509Certificates;
namespace ListCertificates
{
class Program
{
static void Main(string[] args)
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certificates = store.Certificates;
foreach (var certificate in certificates)
{
Console.WriteLine("Friendly Name: \t "+ certificate.FriendlyName);
Console.WriteLine("Subject Name: \t" + certificate.SubjectName.Name);
Console.WriteLine("Subject: \t"+ certificate.Subject);
Console.WriteLine("Serial Number : \t" + certificate.SerialNumber);
}
string answer = String.Empty;
do
{
Console.WriteLine("Would you to search for a cert?");
answer = Console.ReadLine();
if (answer.ToLower() == "yes")
{
Console.WriteLine("Field?");
string f = Console.ReadLine();
Console.WriteLine("Value?");
string value = Console.ReadLine();
X509Certificate2Collection collection = null;
switch (f.ToLower())
{
case "serial":
collection = store.Certificates.Find(X509FindType.FindBySerialNumber, value, true);
break;
case "subjectname":
collection = store.Certificates.Find(X509FindType.FindBySubjectName, value, true);
break;
}
Console.WriteLine((collection == null || collection.Count > 0) ? "Found" : "Not Found");
}
} while (answer.ToLower() != "no");
store.Close();
Console.WriteLine("Press a key to continue.");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment