Mentalis.org Security Library documentation

Certificate Services

Opening certificate Stores

There are many ways to open a certificate store. You can create certificate stores from .cer or .pfx/.p12 files, but a third common method to open certificate stores is to open one by using its name. This example will explain how a certificate store can be opened by its name, and it will also show how to enumerate all server authentication certificates that are located in this store.

using System;
using Org.Mentalis.Security.Certificates;

/// <summary>
/// Returns a list of server authentication certificates from a specified store.
/// </summary>
/// <param name="storeName">The name of the store.</param>
/// <returns>An array of server authentication certificates.</returns>
/// <exception cref="ArgumentNullException">storeName is a null reference
/// (Nothing in Visual Basic).</exception>
/// <exception cref="ArgumentException">storeName is invalid.</exception>
/// <exception cref="CertificateException">An error occurs while loading
/// the specified store</exception>
public Certificate[] EnumServerCerts(string storeName) {
    if (storeName == null)
        throw new ArgumentNullException("storeName", "You must specify a name.");
    if (storeName.Length == 0)
        throw new ArgumentException("Invalid store name.");
    // open the specified certificate store
    CertificateStore store = new CertificateStore(storeName);
    // enumerate all the server authentication certificates
    // "1.3.6.1.5.5.7.3.1" is the server authentication OID
    return store.EnumCertificates(new string[] {"1.3.6.1.5.5.7.3.1"});
}