If you have a certificate and associated private key (as an X509Certificate2
object), you can check certificate.PrivateKey.CspKeyContainerInfo.HardwareDevice
property chain (HardwareDevice property). If the property returns true
then the key is stored on a smart card and most likely (but not necesaary) will propmpt a PIN when you try to access the key. This will work if the key is stored in the legacy Cryptographic Service Provider (CSP).
Modern cards support modern provider types called Key Storage Provider (KSP) which is poorly supported in .NET. This means that if the key is stored in the hardware KSP, then HasPrivateKey
property (of the X509Certificate2
object) will return True
and PrivateKey
will be null
. In this case, you will have to do some extra work by calling native CryptoAPI functions in the NCrypt family. Though, starting with .NET 4.6+, there are several extension methods in X509Certificate2
class which can be used to retrieve provider information: X509Certificate2 Extension Methods.