Click here to Skip to main content
15,882,114 members
Articles / Programming Languages / C# 5.0
Tip/Trick

Get Certificates from the System Store

Rate me:
Please Sign up or sign in to vote.
4.72/5 (8 votes)
15 Oct 2014CPOL 32.7K   11   4
Retrieve certificates from the system certificate store

Introduction

I was working on some code today that required me to retrieve a file from a secure web site on a CAC (smart card) controlled intranet. I found out how to do it, but developed the following code that turned out to be not needed. There are two methods - one that retrieves certificates for the current user from the system certificate store, another that determines if the specified certificate was retrieved from a smart card, and finally, a method that simply lists all categories of certificates found in the store.

The Code

C#
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;

/// Gets the current user certificates from the x509 store.
public static List<X509Certificate2> GetCurrentUserCertificates()
{
    List<X509Certificate2> certificates = new List<x509certificate2>();
    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    store.Open(OpenFlags.OpenExistingOnly);
    foreach(X509Certificate2 cert in store.Certificates)
    {
        certificates.Add(cert);
    }
    return certificates;
}

/// Determines whether the specified certificate was retrieved from a smart card
public static bool IsFromSmartCard(X509Certificate2 certificate)
{
    bool result = (certificate.HasPrivateKey);
    if (result)
    {
        RSACryptoServiceProvider rsa = certificate.PrivateKey as RSACryptoServiceProvider;
        if (rsa != null && rsa.CspKeyContainerInfo.HardwareDevice)
        {
            result = true;
        }
    }
    return result;
}</x509certificate2>

History

  • 15 Oct 2014 - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
QuestionWhere's the third method? Pin
Brisingr Aerowing4-Jun-15 12:25
professionalBrisingr Aerowing4-Jun-15 12:25 
Questiongood article Pin
nes1541115-Oct-14 6:48
nes1541115-Oct-14 6:48 
AnswerRe: good article Pin
#realJSOP17-Oct-14 2:23
mve#realJSOP17-Oct-14 2:23 
GeneralMy vote of 5 Pin
Southmountain15-Oct-14 6:31
Southmountain15-Oct-14 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.