In Windows, we can view and manage the certificates using certmgr.msc, which is a system management console included in Windows itself. 

However, what if we want to view the certificates information in an UWP app? Here is the answer.

First thing first, the method I use does not apply to UWP only. In fact it is from a WPF app I wrote. As Microsoft released .NET Standard 2.0, which allows me to reuse the core library shared between WPF and UWP, that is how I finally come up with an UWP demo app.

For reference, please see the sample app on my GitHub: https://github.com/EdiWang/Cert-Scanner 

Let's dig in into the code. The core piece of this demo is the following code.

The namespace we use is "System.Security.Cryptography.X509Certificates" so make sure you add the using statement if you want to try the code yourself.

public class SystemStorageCertificationScanner : CertificationScanner
{
    public override IEnumerable<CertInfo> ScanCertificates()
    {
        foreach (StoreLocation loc in Enum.GetValues(typeof(StoreLocation)))
        {
            foreach (StoreName n in Enum.GetValues(typeof(StoreName)))
            {
                X509Store store = new X509Store(n, loc);
                store.Open(OpenFlags.ReadOnly);
                foreach (var storeCertificate in store.Certificates)
                {
                    var certInfo = new CertInfo()
                    {
                        Subject = storeCertificate.Subject,
                        FriendlyName = storeCertificate.FriendlyName,
                        Issuer = storeCertificate.Issuer,
                        Version = storeCertificate.Version,
                        Thumbprint = storeCertificate.Thumbprint,
                        StoreLocation = loc.ToString(),
                        ExpDate = DateTime.Parse(storeCertificate.GetExpirationDateString()),
                        Abstract = storeCertificate.ToString()
                    };
                    yield return certInfo;
                }
                store.Close();
            }
        }
    }
}

In this example, StoreLocation is an enum which only contains 2 members: CurrentUser, LocalMachine

StoreName is another enum which stands for different type names for the installed certificates. It's members are:

public enum StoreName
{
  AddressBook = 1,
  AuthRoot = 2,
  CertificateAuthority = 3,
  Disallowed = 4,
  My = 5,
  Root = 6,
  TrustedPeople = 7,
  TrustedPublisher = 8,
}

And then we can get all the certificates by enumerate every X509Store object and the certificates inside it. But please notice, we will have to open and close the X509Store everytime we do an operation on it. 

CertInfo is a custom type I wrote just to make things easier to read and use.

public class CertInfo
{
    public string Subject { get; set; }

    public string FriendlyName { get; set; }

    public string Issuer { get; set; }

    public string Thumbprint { get; set; }

    public int Version { get; set; }

    public string StoreLocation { get; set; }

    public DateTime ExpDate { get; set; }

    public bool IsExpired => ExpDate < DateTime.Now;

    public string Abstract { get; set; }
}

To polish the design, I added couple of interface and abstract class. They are just for design reference, in fact if you just want a straight forward method to read certificates, you won't need any of these.

public interface ICertificationScanner
{
    IEnumerable<CertInfo> ScanCertificates();
}

public abstract class CertificationScanner : ICertificationScanner
{
    public abstract IEnumerable<CertInfo> ScanCertificates();
}

The UWP app I created is using Windows Template Studio, it provides me the Telerik Data Grid, which makes it easy to display the certificates list. I am also using MvvmLight as design pattern. I won't repeat the steps for creating Apps in WTS because it is irrelevant to this topic.

Let's say you already have Telerik Data Grid in your app. Then, to display the result, the most simple way is add one line of XAML on the UI:

<tg:RadDataGrid ColumnDataOperationsMode="Flyout" x:Name="grid" ItemsSource="{x:Bind ViewModel.Source}" />

and retrieve the data in ViewModel

public class SystemCertsViewModel : ViewModelBase
{
    public SystemStorageCertificationScanner SysCertificationScanner { get; set; }

    public SystemCertsViewModel()
    {
        SysCertificationScanner = new SystemStorageCertificationScanner();
    }

    public ObservableCollection<CertInfo> Source => SysCertificationScanner.ScanCertificates().ToObservableCollection();
}

Here in this example, the ToObservableCollection extension is provided my UWP Helper library. You can install it from NuGet.

PM> Install-Package Edi.Uwp.Helpers

Now, the result screen is:

Finally, to get the full example, please see my GitHub: https://github.com/EdiWang/Cert-Scanner