証明書の秘密キーがハードウェアデバイスに格納されているかどうかを調べたいと思います。
はのは、私が探している情報がr.CspKeyContainerInfo.HardwareDevice
にある次のアプリケーション秘密鍵が、デバイスが存在しないハードウェアデバイスにあるかどうかを確認します。
class Program
{
static void Main(string[] args)
{
try
{
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
foreach (X509Certificate2 x509 in store.Certificates)
{
if (x509.HasPrivateKey)
{
AsymmetricAlgorithm a = x509.PrivateKey;
RSACryptoServiceProvider r = a as RSACryptoServiceProvider;
if (null != r)
{
System.Console.WriteLine("hardware: " + r.CspKeyContainerInfo.HardwareDevice);
System.Console.WriteLine("Subject: " + x509.Subject);
System.Console.WriteLine("container: " + r.CspKeyContainerInfo.KeyContainerName);
System.Console.WriteLine("---");
}
}
}
}
catch (CryptographicException ex)
{
Console.WriteLine("Information could not be written out for this certificate.");
}
}
}
を想定してみましょう。
残念ながら、ベーススマートカードcspによって提供される店舗では、AsymmetricAlgorithm a = x509.PrivateKey
が実行されると(その時点でスマートカードが存在しない場合)、デバイスを挿入するように求められます。
この迷惑な「スマートカードを挿入してください」というダイアログが表示されない限り、同じ情報を取得する方法はありますか?